-
Notifications
You must be signed in to change notification settings - Fork 534
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
- The argument of
fitz.Pixmap(arg)
can be a file or abytes
orio.BytesIO
object containing a file image - Instead of creating an output file like above, you can also create a bytes object via
pix.getImageData("yyy")
and pass this around. - 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. - Since v1.14.6,
pix.writeImage()
no longer requires an extra format parameter: the format is inferred from the filename's extension.
import fitz
pix = fitz.Pixmap("myfamily.jpg")
pix.writeImage("myfamily.psd")
from PIL import Image
import fitz
pix = fitz.Pixmap(...)
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
img.save("output.jpg", "JPEG")
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
HOWTO Button annots with JavaScript
HOWTO work with PDF embedded files
HOWTO extract text from inside rectangles
HOWTO extract text in natural reading order
HOWTO create or extract graphics
HOWTO create your own PDF Drawing
Rectangle inclusion & intersection
Metadata & bookmark maintenance