Skip to content

Question / Comment: Setting xres/yres for new image pages #479

Closed
@wohali

Description

@wohali

Hi there,

PyMuPDF is great! Thanks for the best PDF support in Python on the planet.

I'm currently working on some code to straighten scanned images in PDFs. I use PyMuPDF, imageio and numpy/scikit to do most of the heavy lifting.

Following the guide, I can't figure out how restore the original xres/yres of the extracted image.

Code excerpt, very similar to your example:

doc = fitz.open(filename)
imgdict = doc.extractImage(xref)
img = imread(imgdict["image"])
# image is manipulated, then:
imgbytes = imwrite("<bytes>", img, format=imgext)
imgdoc = fitz.open(stream=imgbytes, filetype=imgext)
rect = imgdoc[0].rect
pdfbytes = imgdoc.convertToPDF()
imgdoc.close()
imgPDF = fitz.open("pdf", pdfbytes)
page = newdoc.newPage(width=rect.width, height=rect.height)
page.showPDFpage(rect, imgPDF, 0)

I think the issue is in the fitz.open() call. I don't see any way to pass in xres/yres (ppi) options to the constructor. No matter what I do, I end up with 96ppi. (Certain "fruit-branded" PDF readers care about this value a lot.)

How do I retain the original xres/yres values when creating a new document from an image byte stream?

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions