Skip to content

DPI is a tuple - #2472

Merged
wiredfool merged 11 commits into
python-pillow:masterfrom
hugovk:dpi-is-a-tuple
Apr 3, 2017
Merged

wiredfool merged 11 commits into
python-pillow:masterfrom
hugovk:dpi-is-a-tuple

Conversation

@hugovk

@hugovk hugovk commented Apr 1, 2017

Copy link
Copy Markdown
Member

Fix merged PR #2449, re: 977f319#commitcomment-21546844

DPI should be an (xres, yres) tuple.

Confirmed in code and docs:

The open() method may set the following info properties if available:
...
dpi
A tuple representing the reported pixel density in pixels per inch, if the file is a jfif file and the units are in inches.
...
The save() method supports the following options:
...
dpi
A tuple of integers representing the pixel density, (x,y).

Note: one test image (Tests/images/exif_gps.jpg) only has an X value, and so raised exceptions, so if there's no Y value, use the X twice.

hugovk referenced this pull request Apr 1, 2017
If DPI isn't in JPEG header, fetch from EXIF
Comment thread PIL/JpegImagePlugin.py Outdated
x_resolution = self._getexif()[0x011A]
self.info["dpi"] = x_resolution[0] / x_resolution[1]
exif = self._getexif()
x_resolution = exif[0x011A]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could fail with a keyerror?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Theoretically, yes. I'll wrap in another try/except later.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, but see below.

@hugovk

hugovk commented Apr 1, 2017 •

Copy link
Copy Markdown
Member Author

It's better to check the spec before implementing things...

http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/EXIF.html says:

  • Exif.Image.XResolution: "The number of pixels per <ResolutionUnit> in the <ImageWidth> direction. When the image resolution is unknown, 72 [dpi] is designated."
  • Exif.Image.YResolution: "The number of pixels per <ResolutionUnit> in the <ImageLength> direction. The same value as <XResolution> is designated."
  • Exif.Image.ResolutionUnit: "The unit for measuring <XResolution> and <YResolution>. The same unit is used for both <XResolution> and <YResolution>. If the image resolution is unknown, 2 (inches) is designated."

And http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/EXIF.html gives these for resolution unit:

(the value 1 is not standard EXIF)
1 = None
2 = inches
3 = cm

So I'll update this again:

  • we don't need to check for YResolution, it's the same as XResolution - is that correct?
  • if no value, default to 72,72 dpi
  • should also check the unit: if inches, do nothing; if cm, convert to inches; else default to inches

@wiredfool

Copy link
Copy Markdown
Member

@hugovk , you happy with this?

@hugovk

hugovk commented Apr 3, 2017

Copy link
Copy Markdown
Member Author

@wiredfool Yes.

Feel free to squash and merge if you'd like to tidy up the commits.

image

@wiredfool

Copy link
Copy Markdown
Member

someone is screen shotting on a high dpi monitor...

@wiredfool
wiredfool merged commit 53df626 into python-pillow:master Apr 3, 2017
@hugovk
hugovk deleted the dpi-is-a-tuple branch April 4, 2017 03:15
@hugovk

hugovk commented Apr 4, 2017

Copy link
Copy Markdown
Member Author

Yes, that was a surprisingly large image!

@radarhere

radarhere commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

we don't need to check for YResolution, it's the same as XResolution - is that correct?

#10334 is poised to start reading Y resolution again, in case anyone here would like to weigh in.

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.

3 participants