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

for some JPEG files PIL does not return DPI, but returns jfif_density #40

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/img2pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -1313,9 +1313,18 @@ def get_imgmetadata(

ndpi = imgdata.info.get("dpi")
if ndpi is None:
# See: https://github.com/python-pillow/Pillow/issues/8632
if imgformat == ImageFormat.JPEG and imgdata.info.get("jfif_density") is not None:
jfif_density = imgdata.info.get("jfif_density")
jfif_unit = imgdata.info.get("jfif_unit", 0)
if jfif_unit == 1:
ndpi = jfif_density
elif jfif_unit == 2:
ndpi = (jfif_density[0] * 2.54, jfif_density[1] * 2.54)

# the PNG plugin of PIL adds the undocumented "aspect" field instead of
# the "dpi" field if the PNG pHYs chunk unit is not set to meters
if imgformat == ImageFormat.PNG and imgdata.info.get("aspect") is not None:
elif imgformat == ImageFormat.PNG and imgdata.info.get("aspect") is not None:
aspect = imgdata.info["aspect"]
# make sure not to go below the default dpi
if aspect[0] > aspect[1]:
Expand Down