-
Notifications
You must be signed in to change notification settings - Fork 587
How to Convert Images
Jorj X. McKie edited this page Jan 6, 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 |
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 |
TGA | TGA | Targa Image File |
- | 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") # yyy is any of the supported output formats
- The argument of
fitz.Pixmap(arg)
can be a file or a bytes 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.
import fitz
pix = fitz.Pixmap("myfamily.jpg")
pix.writeImage("myfamily.psd", "psd")
from PIL import Image
import fitz
pix = fitz.Pixmap(...)
if pix.alpha: # this has transparency bytes!
pix = fitz.Pixmap(pix, 0) # we must strip transparency channel for JPEG
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