Pymupdf: Question / Comment: Size of generated images.

Created on 27 Jun 2020  路  4Comments  路  Source: pymupdf/PyMuPDF

Hi, I would like to know if it is expected when converting an A4 size PDF to PNG, the image is being generated as 596x842, as the A4 standard is 595x842.

For all tested resolutions, the width is 1 pixel larger.

72 dpi = 596x842
96 dpi = 795 x 1123
150 dpi = 1241 x 1754
300 dpi = 2481 x 3508

Code:

# For creating 300 DPI resolution images using PyMuPDF
DESIRED_INCREASE = 300/ 72
mat = fitz.Matrix(DESIRED_INCREASE, DESIRED_INCREASE)

doc = fitz.open(pdf_path)
    for page in doc:
      pix = page.getPixmap(matrix=mat)
      pix.writePNG("page-%i.png" % page.number)

My settings:
Win10
Python 3.8

question resolved

Most helpful comment

Oh now I see, thanks for the explanation, and for the amazing work on this project!

All 4 comments

Image / pixmap dimensions of page images are created from page.rect.irect.
Because an A4 page is _not_ exactly 595 x 842, but something like Rect(0.0, 0.0, 595.3200073242188, 841.9199829101562), the IRect of that Rect is IRect(0, 0, 596, 842).

To see how IRect calculation works, consult the documentation of method Rect.bound(). These chapters are definitely worth reading, because not everything around emptiness, finiteness, containment and intersection of geometry objects always conforms with "intuition".

Maybe I should add that if indead a page's size has integer dimensions, then the pixmap will also have an IRect with these values.

Oh now I see, thanks for the explanation, and for the amazing work on this project!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dothinking picture dothinking  路  4Comments

gustavocidornelas picture gustavocidornelas  路  4Comments

jkrankl picture jkrankl  路  3Comments

suvinks picture suvinks  路  5Comments

cherryjo18 picture cherryjo18  路  3Comments