From 21c7ac8d6ab53f4fe9a5ba449feafa904b3863ca Mon Sep 17 00:00:00 2001 From: Hugo Osvaldo Barrera Date: Tue, 11 Aug 2020 03:08:25 +0200 Subject: [PATCH] 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 --- Tests/images/exif-gps-ifd-missing.jpeg | Bin 0 -> 1087 bytes Tests/test_file_jpeg.py | 8 ++++++++ src/PIL/TiffImagePlugin.py | 9 +++++++-- 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 Tests/images/exif-gps-ifd-missing.jpeg diff --git a/Tests/images/exif-gps-ifd-missing.jpeg b/Tests/images/exif-gps-ifd-missing.jpeg new file mode 100644 index 0000000000000000000000000000000000000000..28f180b8743a97318161e5caa47e5bcdc3fd5fbc GIT binary patch literal 1087 zcmb7DL2DC16n^t&wux!6*|aLugKI$)Pn&H_(i{{NiwZ3mFCu!dq{1Fb5kx!}rNN8o z+*(kUDvEm23I_3DwI^@hs$TRUcoF;o>iXX9>?BApzGQa3{pNdb-n@AmZ^d7rcxLMK z6bK>U6#XFnihoYt2(JK4Ps3pV$it8Tkc7l=L=TbT!7dYk#OG+-6U7AYB>Yg6QVk4{ zW(*wSC@!0bcSTNWDfkmZ}_!FQ1i-_My1-Q)B*M_u4Z{)bMdal7uw^!oZ1Id+@f%u z^o+_60>2}(TyeIeEWW-hk~%1X610Fqng!n0Xya=wEx)Rj#cSHJnkPEf%R%Q2-UXm{ z7+fS=u=?k^ljJ~mfUoqj#c#E?_ KD?hxY_~Tzl{d_zC literal 0 HcmV?d00001 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