Don't raise IndexError when GPS IFD tag is missing

When images are missing this tag (this is apparently common for many
images generated by phones), running them through
`ImageOps.exif_transpose` raises `IndexError`.

The root cause here is they're missing some tags in the EXIF metadata,
and this isn't handled very gracefully.

Fixes #4215
This commit is contained in:
Hugo Osvaldo Barrera 2020-08-11 03:08:25 +02:00
parent 398a591e7e
commit 21c7ac8d6a
3 changed files with 15 additions and 2 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@ -735,6 +735,14 @@ class TestFileJpeg:
# Assert the entire file has not been read
assert 0 < buffer.max_pos < size
def test_missing_gps_ifd(self):
# This image is missing its gps ifd data.
with Image.open("Tests/images/exif-gps-ifd-missing.jpeg") as im:
# This should return the default, and not a IndexError when a tag is
# missing.
assert im.info.get("dpi") == (72, 72)
@pytest.mark.skipif(not is_win32(), reason="Windows only")
@skip_unless_feature("jpg")

View File

@ -587,15 +587,20 @@ class ImageFileDirectory_v2(MutableMapping):
TiffTags.SIGNED_RATIONAL,
]: # rationals
values = (values,)
try:
if len(values) == 1:
(dest[tag],) = values
except ValueError:
elif len(values) > 1:
# We've got a builtin tag with 1 expected entry
warnings.warn(
"Metadata Warning, tag %s had too many entries: %s, expected 1"
% (tag, len(values))
)
dest[tag] = values[0]
else:
warnings.warn(
"Metadata Warning, tag %s had no entries, expected 1" % tag
)
else:
# Spec'd length > 1 or undefined