Save PDF file as images with same quality as original PDF

Viewed 333

I want to save each page of a pdf file as a single image file:

import fitz
doc = fitz.open('file.pdf')
for i in range(doc.page_count):
    page = doc[i]
    pix = page.get_pixmap()
    pix.save(f'page-{i}.png')
    pix.pil_save(f'page-{i}.jpg', optimize = False, dpi = (1500, 1500))

The images are in worse quality than in the original pdf file, no matter which resolution I choose. How can I save them with the same or a similar quality?

1 Answers

Just a simple configuration, Add the dpi option in get_pixmap()

import fitz
doc = fitz.open('file.pdf')
resolution_parameter = 300
for i in range(doc.page_count):
    page = doc[i]
    pix = page.get_pixmap(dpi = resolution_parameter)
    pix.save(f'page-{i}.png')
    pix.pil_save(f'page-{i}.jpg', optimize = False, dpi = (1500, 1500))
Related