Skip to content

Conversation

@radarhere
Copy link
Member

@radarhere radarhere commented May 7, 2021

Helps #4129

BMP and PNG don't save our dpi value untransformed. Instead, BMP multiplies it by 39.3701 and PNG divides it by 0.0254, and then saves it as an integer. Because this limits the precision, that makes it unlikely that dpi value used to save an image will be the same dpi that is loaded afterwards - this means that this PR abandons the idea that you should be able to roundtrip DPI values. So

from PIL import Image
im = Image.new("RGB", (100, 100))
im.save("out.png", dpi=(50, 50))

im = Image.open("out.png")
print(im.info["dpi"])  # (50.0126, 50.0126)

After that, it removes rounding from JPEG loading as well. This is more of a cleanup than a change though, because as it is an integer when saving, I don't think it is possible to have a JPEG with a float dpi without hexediting the image.

@hugovk hugovk merged commit a72ae68 into python-pillow:master May 16, 2021
@radarhere radarhere deleted the dpi_rounding branch May 16, 2021 22:17
jbarlow83 added a commit to ocrmypdf/OCRmyPDF that referenced this pull request Jul 14, 2021
Pillow decided against round-tripping DPI values.
python-pillow/Pillow#5476

Fixes #802
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

Successfully merging this pull request may close these issues.

2 participants