Which is the fastest way to transfer the pdf pages into separate jpeg images? #1648
-
Dear developers,
doc = fitz.open('xxx.pdf')
p0 = doc.load_page(0)
p0.save('page_0.jpg')
with open('page_0.jpg', 'rb') as infile:
img_bytes = infile.read()
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
Welcome and let me first transform this issue to a Discussions item. |
Beta Was this translation helpful? Give feedback.
-
To make an image of a page, you must first take "picture", which is called "pixmap":
With page = doc[0]
pix = page.get_pixmap(dpi=300) # RGB with a high resolution
pix.pil_save("page-0.jpg") |
Beta Was this translation helpful? Give feedback.
-
Thanks for the quick reply. Is there a way to get the image bytes directly? In your solution, I have to |
Beta Was this translation helpful? Give feedback.
To make an image of a page, you must first take "picture", which is called "pixmap":
pix = page.get_pixmap()
. You can influence many properties of that pixmap, like resolution, whether gray or RGB, rotation or flipping.Once you have that, either use
pix.save(...)
which can only produce PNG, PNM and a handful of other formats, orpix.pil_save()
which uses pillow with an additional internal step. This allows JPEG output among others.With
.pil_save()
you have the full abilities of pillow output available: the method arguments are those of pillow, which you therefore must lookup. Simple example: