Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DPI is rounded wrong by 1 #3323

Closed
fireattack opened this issue Aug 31, 2018 · 4 comments
Closed

DPI is rounded wrong by 1 #3323

fireattack opened this issue Aug 31, 2018 · 4 comments

Comments

@fireattack
Copy link

fireattack commented Aug 31, 2018

What did you do?

Take this image

test_image.zip

Its info according to magick:

>magick identify -verbose 08.bmp
Image: 08.bmp
  Format: BMP3 (Microsoft Windows bitmap image (V3))
  Class: DirectClass
  Geometry: 518x244+0+0
  Resolution: 177.17x177.17
  Print size: 2.92375x1.37721
  Units: PixelsPerCentimeter
  Type: TrueColor
  Endianess: Undefined
  Colorspace: sRGB

As you can see, the PPC is 177.17, which converts to 177.17*2.54=450.0118 DPI

What did you expect to happen?

When use img.info["dpi"], it should show (450,450)

What actually happened?

It shows (451,451)

What versions of Pillow and Python are you using?

Python 3.6.5
Pillow 5.2.0

Repro code

from PIL import Image

img = Image.open("08.bmp")
print(img.info["dpi"])
@radarhere
Copy link
Member

The dpi was set to round up in https://github.com/python-pillow/Pillow/pull/745/files#diff-3b431a4460b330394080ef7c1391f9e5R101. So this was deliberate, rather than being accidental, and has been part of Pillow for four years. There is a general reluctance to change established behaviour. Can you provide a compelling reason why the rounding should change?

@fireattack
Copy link
Author

fireattack commented Aug 31, 2018

I won't self-claim it's "compelling" but here is my thought.

  1. Rounding to nearest number is better than ceiling in term of accuracy.
  2. The additional computation cost is negligible, if any.
  3. Most importantly, this problem is actually fairly common (instead of being some obscure use cases). Often times, DPI metadata is stored in metric unit as you can see here (I don't know if it's the only way to do so, but all the scanners I ever use do this when saving image files), so pillow has to convert it with rounding.

In my personal use case, I use pillow to batch convert image format, so I want to keep all metadata as pristine as before. (Kinda off-topic, but I currently just call external program (magick) as a workaround.)

@radarhere
Copy link
Member

radarhere commented Mar 11, 2019

I've created PR #3709, which resolves this.

@fireattack
Copy link
Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants