Skip to content

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
Clone this wiki locally