Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to Convert Images

Jorj X. McKie edited this page Sep 28, 2019 · 6 revisions

Just as a feature among others, dealing with images using PyMuPDF is easy. It may avoid using other graphics packages like PIL/Pillow in many cases. Notwithstanding that interfacing with Pillow is almost trivial.

Input Formats Output Formats Description
JPEG - Joint Photographic Experts Group
BMP - Windows Bitmap
JXR - JPEG Extended Range
JPX - JPEG 2000
GIF - Graphics Interchange Format
TIFF - Tagged Image File Format
PNG PNG Portable Network Graphics
PNM PNM Portable Anymap
PGM PGM Portable Graymap
PBM PBM Portable Bitmap
PPM PPM Portable Pixmap
PAM PAM Portable Arbitrary Map
- PSD Adobe Photoshop Document
- PS Adobe Postscript

The general scheme is as simple as follows:

import fitz
# ...
pix = fitz.Pixmap("input.xxx") # input.xxx: a file in any of the supported input formats
pix.writeImage("output.yyy") # yyy is any of the supported output formats

Remarks

  1. The argument of fitz.Pixmap(arg) can be a file or a bytes or io.BytesIO object containing a file image
  2. Instead of creating an output file like above, you can also create a bytes object via pix.getImageData("yyy") and pass this around.
  3. As a matter of course, input and output formats must be compatible in terms of colorspaces and transparency. The Pixmap class has batteries included for cases, where this is not so.
  4. Since v1.14.6, pix.writeImage() no longer requires an extra format parameter: the format is inferred from the filename's extension.

Example: Convert JPEG to Photoshop

import fitz
pix = fitz.Pixmap("myfamily.jpg")
pix.writeImage("myfamily.psd")

Example: Output JPEG via PIL/Pillow

from PIL import Image
import fitz
pix = fitz.Pixmap(...)
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
img.save("output.jpg", "JPEG")

Example: Convert JPEG to Tkinter PhotoImage

import fitz
if str is bytes: # this is Python 2!
 import Tkinter as tk
else: # Python 3 or later!
 import tkinter as tk
pix = fitz.Pixmap("input.jpg")
tkimg = tk.PhotoImage(data=pix.getImageData("ppm")) # PPM is among the tk-supported formats

Recipes

HOWTO Button annots with JavaScript

HOWTO extract images

HOWTO join PDFs

HOWTO work with PDF embedded files

HOWTO Convert Images

HOWTO extract text from inside rectangles

HOWTO extract text in natural reading order

HOWTO add PDF form fields

HOWTO deal with annotations

HOWTO convert to PDF

HOWTO show PDF Form fields

HOWTO work with vector images

HOWTO create or extract graphics

HOWTO create your own PDF Drawing

HOWTO add pages, images, text

HOWTO extract fonts

HOWTO rearrange pages

HOWTO GUI PDF display

Algebra with geometry objects

Rectangle inclusion & intersection

Hyperlink maintenance

Visual table extraction

Incremental saves

Metadata & bookmark maintenance

Wrapping FileOptimizer

Installation

Ubuntu

Ubuntu Installation Experience

Windows Binaries

Windows Binaries Generation

Windows Binaries Installation

Clone this wiki locally

AltStyle によって変換されたページ (->オリジナル) /