Replies: 8 comments 3 replies
-
FYI I attempted your recent workaround and got the same result, sadly: >>> for page in doc:
for img in [b for b in page.get_text("dict")["blocks"] if b["type"] == 1]:
print(f"{img['xres']=}, {img['yres']=}")
pix = pymupdf.Pixmap(img["image"])
print(f"{pix.xres=}, {pix.yres=}") # <== this is correct!
print()
img['xres']=96, img['yres']=96
pix.xres=96, pix.yres=96
img['xres']=96, img['yres']=96
pix.xres=96, pix.yres=96
img['xres']=96, img['yres']=96
pix.xres=96, pix.yres=96
img['xres']=96, img['yres']=96
pix.xres=96, pix.yres=96 Reading the upstream bug implied the underlying library can't do much to help here, but, we should be able to do better in PyMuPDF and calculate the x/y ppi as used within the context of the PDF itself as poppler does. poppler performs the calculation here. Using this information: >>> import pymupdf
>>> from math import sqrt
>>> doc = pymupdf.open("i-9-paper-version.pdf")
>>> p1 = Document.load_page(1)
>>> for img in p1.get_image_info():
... mat = img['transform']
... width2 = sqrt(pow(mat[0], 2) + pow(mat[1], 2))
... height2 = sqrt(pow(mat[2], 2) + pow(mat[3], 2))
... xppi = round(abs(img['width'] * 72 / width2))
... yppi = round(abs(img['height'] * 72 / height2))
... print(f"image #{img['number']}, xppi: {xppi}, yppi: {yppi}")
...
image #1, xppi: 300, yppi: 301
image #35, xppi: 300, yppi: 304
image #36, xppi: 300, yppi: 304 This is the information I'm after. I can carry this code in my application, but would you consider having PyMuPDF return this information when retrieving images from PDFs instead? |
Beta Was this translation helpful? Give feedback.
-
During the discussions around this question (not even an issue), it became clear that the currently returned values do not report worthwhile information at all. Only two aspects can potentially be of interest:
The first definition is an image property which thus remains constant across all potential places where this same image may be displayed in the PDF. The second definition obviously is bbox-specific and could therefore be different for two bboxes showing the same image on the same page. It does not reflect an image property. |
Beta Was this translation helpful? Give feedback.
-
When I take your image at xref 48 and insert it in a new page like that: import pymupdf
doc = pymupdf.open()
page = doc.new_page()
r1 = pymupdf.Rect(100, 100, 200, 200)
r2 = pymupdf.Rect(200, 200, 400, 400)
xref = page.insert_image(r1, filename="48.png")
page.insert_image(r2, xref=xref)
doc.ez_save("x.pdf") Your suggested computation will deliver these values for the exact same image:
I'm pretty sure that immediately after such a potential change, we would see issues asking why this can happen for the same image 😏. |
Beta Was this translation helpful? Give feedback.
-
I cannot say yet whether we will implement your suggestion. If we would however, there would be consequential documentation and potentially other changes: |
Beta Was this translation helpful? Give feedback.
-
Thanks for the serious consideration, @JorjMcKie . My specific application, and those who use it, is the processing of output from document scanners. My process needs to extract images, perform processing of them, and place them into another PDF. In this specific case, all pages consist of a single document with a trivial bbox (though sometimes with x/y offsets in As a result, the problem you mention would never arise in my application, though it is a special case of 1 page == 1 image. However, not all scanned documents in my workflow are the same dimensions, so it is not possible to assume that everything is A4 / Letter / etc. |
Beta Was this translation helpful? Give feedback.
-
Thanks for your most recent post. Not sure I'm 100% understanding though 😟. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Applying your suggested revised formula for resolution computation in this example delivers The original image dimensions are width = 439, height = 501. The distance between the top-left and the top-right points in the rotated / tilted image is 193. This is easy to compute:
|
Beta Was this translation helpful? Give feedback.
-
Description of the bug
Continuing the discussion from #479, I'd expected this to work, but it still doesn't:
vs.
At the moment, I am forced to shell out to pdfimages to retrieve the per-image xres/yres. Is this something possible in the future with pymupdf at all?
How to reproduce the bug
Run the code above with the attached PDF file.
i-9-paper-version.pdf
PyMuPDF version
1.25.5
Operating system
Linux
Python version
3.11
Beta Was this translation helpful? Give feedback.
All reactions