diff --git a/Tests/images/exif-gps-ifd-missing.jpeg b/Tests/images/exif-gps-ifd-missing.jpeg new file mode 100644 index 000000000..28f180b87 Binary files /dev/null and b/Tests/images/exif-gps-ifd-missing.jpeg differ diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 03a2dba51..2fad3b812 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -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") diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index 6a772e48b..a282a23f1 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -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