How to convert PDF to image, using the original document settings for the image size and set to 300dpi? #1307
-
Hi, I'm currently looking at using PyMuPDF for a workflow that converts PDF's to images (In my case, .TIFF files). I am trying to mimic the behaviour of another program that I currently use for PDF -> Image conversion. In that program, it lets you set the settings for imaging as below: My question is, is this possible within PyMuPDF? How can I set the output DPI for my images to 300 and set the image size to the original document size? I am quite new to dealing with this sort of processing for PDF's/images so any help would be much appreciated. Thanks in advance, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Sounds like you will create a so-called "pixmap" for each page and save that as an image. import fitz
mat = fitz.Matrix(300 / 72, 300 / 72) # sets zoom factor for 300 dpi
doc = fitz.open("yourfile.pdf")
for page in doc:
pix = page.get_pixmap(matrix=mat)
img_filename = "page-%04i.tiff" % page.number
pix.pil_save(img_filename, format="TIFF", dpi=(300,300), ... more PIL parameters) For more sophistication on PIL output, please consult their documentation. For example, TIFF supports multiple images in one file. |
Beta Was this translation helpful? Give feedback.
Sounds like you will create a so-called "pixmap" for each page and save that as an image.
PyMuPDF itself only support a handful of image output formats, the most popular being PNG, others are the PNM-type images.
If you want to use others, you must use an additional package, presumably PIL/Pillow.
PyMuPDF supports Pillow directly via its pixmap output methods.
So a code snippet may look like this: