From bcc766e02da39d94aa01f91422596efd4d3219e4 Mon Sep 17 00:00:00 2001 From: Bei Pang Date: Thu, 17 Oct 2019 10:02:15 -0700 Subject: [PATCH 1/3] Use default DPI when exif provides invalid x_resolution --- .../images/invalid-exif-without-x-resolution.jpg | Bin 0 -> 2546 bytes Tests/test_file_jpeg.py | 8 ++++++++ src/PIL/JpegImagePlugin.py | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 Tests/images/invalid-exif-without-x-resolution.jpg diff --git a/Tests/images/invalid-exif-without-x-resolution.jpg b/Tests/images/invalid-exif-without-x-resolution.jpg new file mode 100644 index 0000000000000000000000000000000000000000..00f6bd2f30541c65db4a1d2792cd1197661bb9f0 GIT binary patch literal 2546 zcmcIlU2GIp6uz^&`~*p1Z3|_)4VM8n4eajx?ca=@lKra`U0d5#kOY$H%(NZ4v$M`l z>2^uf2NPZiKTjs0(S*bh2>4_Ok*HDB0MQ2m_+WT2;Q=rqLSodcXJ&sW&@E5iQ;RHkZqe;nAHV`4Qk;hR~4}$O9V9 zvb6h%3w49;f%7~K-3PksSi+BecM>xG_jL%~U3Z zvCL58Ecg=1Mo@EebKyBIY?xCN!}B~vvlPpQ01+zB7cHq0Dwa1l88T{FHnoza8AWU} zN)yJcC6eH&6M`bQSWB~J!QoV7N-e0mTC~cLF2;_^!KzTD**z`1td*qjff8jeJ85m= zAW1FIqWNzEOU=St9NyY~mOYnS+@N{Cy591+Y>lZ;61S)~<@|g}rN-2A^yz&Ddv zk%dXa)FsH2rb|;Qb)ck9k+_}#;dQlG&KZhY5XmgInef@N&;+Zlk*wL zQbn?D2i2izQw3NH8VfOOi01Om;&1RlaVe4|wP1_)GM5#hl6Igp482+T}5v5lIL)tZ0hBWwY@_nvP_7mSdPKpNPaWsRW;iC*w?nrz4JU zU^`qaTT)S0o3`2$Tdu7wbOYRoY^TaF8?7;}zT40W+Te4K(Ew!^JM7@PR!37$v4HD# zFsi8?RFxsq(6Li3g0{fL6qjbWIL)T`NH)dr8J4C~$y6lDCh6!9m&lS08)ZRCUT8x# zy;{(m%_Zn~TWvix@X>m#)FRxA2~Sb&KB_)>wYe~j5~<;w$A110O8JSs$lbg3Rlf_~ zTIBM(Mv?cq1o>TYw9>v~hNU^bYXH5(FibqcZKq*4Y+D_+wZB~3fBf#rwbu@PdH7!c z-oewg2dKlTDx_Qn1X24HeTZJ}=;&C}(Y0nxS6`>Mvya@+)wO{ndwP65J!GJ76ULkR z0)Y_4QlY@WKr(61rv}m4*}1M~UGMt!y}s@Z-9GH|`+bvq6PH zEb2WU8JE4!Fu?!IZM51=z%gzw+JS!1tMBZd`n>;!E;!Q8N}ECKzy9g5gVm$kZ+-LK z_koQ!sz=)Z?Pb^1bFXc?_bBVTdTt~PvS$fw@b;e%A6(DBd8Q#hXpaS2hwlHQb-aFo zw-?Sj-dc`a%G*n)ZMrSHzqtGRzLTFVfkfw~e*W>IINV)$@1Kv39op4C18CLbeXCDx xJo9MFTj&3NSRA?EzXW#M&42%j2Oghz`=?Lrn_I#G*QHbG0JypY8G65V>p$U+Z&?5U literal 0 HcmV?d00001 diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 5a34a3faa..2d9864d9a 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -634,6 +634,14 @@ class TestFileJpeg(PillowTestCase): # OSError for unidentified image. self.assertEqual(im.info.get("dpi"), (72, 72)) + def test_invalid_exif_x_resolution(self): + # When no x or y resolution defined in EXIF + im = Image.open("Tests/images/invalid-exif-without-x-resolution.jpg") + + # This should return the default, and not a ValueError or + # OSError for unidentified image. + self.assertEqual(im.info.get("dpi"), (72, 72)) + def test_ifd_offset_exif(self): # Arrange # This image has been manually hexedited to have an IFD offset of 10, diff --git a/src/PIL/JpegImagePlugin.py b/src/PIL/JpegImagePlugin.py index da0759129..62c8d6281 100644 --- a/src/PIL/JpegImagePlugin.py +++ b/src/PIL/JpegImagePlugin.py @@ -172,7 +172,7 @@ def APP(self, marker): # 1 dpcm = 2.54 dpi dpi *= 2.54 self.info["dpi"] = int(dpi + 0.5), int(dpi + 0.5) - except (KeyError, SyntaxError, ZeroDivisionError): + except (KeyError, SyntaxError, ValueError, ZeroDivisionError): # SyntaxError for invalid/unreadable EXIF # KeyError for dpi not included # ZeroDivisionError for invalid dpi rational value From a2b0269167329a110b8ff0ed930ee632c87f6b7a Mon Sep 17 00:00:00 2001 From: Bei Pang Date: Thu, 24 Oct 2019 16:32:11 -0700 Subject: [PATCH 2/3] Added comment to ValueError --- src/PIL/JpegImagePlugin.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/PIL/JpegImagePlugin.py b/src/PIL/JpegImagePlugin.py index 62c8d6281..00ea95e41 100644 --- a/src/PIL/JpegImagePlugin.py +++ b/src/PIL/JpegImagePlugin.py @@ -172,10 +172,11 @@ def APP(self, marker): # 1 dpcm = 2.54 dpi dpi *= 2.54 self.info["dpi"] = int(dpi + 0.5), int(dpi + 0.5) - except (KeyError, SyntaxError, ValueError, ZeroDivisionError): + except (KeyError, TypeError, SyntaxError, ValueError, ZeroDivisionError): # SyntaxError for invalid/unreadable EXIF # KeyError for dpi not included # ZeroDivisionError for invalid dpi rational value + # ValueError for x_resolution[0] being an invalid float self.info["dpi"] = 72, 72 From f53b86b6730aa730c3bf73e5d868ceadecdfd026 Mon Sep 17 00:00:00 2001 From: Bei Pang Date: Mon, 28 Oct 2019 09:48:37 -0700 Subject: [PATCH 3/3] Removed TypeError exception check in JpegImagePlugin; Updated comments in test --- Tests/test_file_jpeg.py | 4 ++-- src/PIL/JpegImagePlugin.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 2d9864d9a..1ad9961a2 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -635,11 +635,11 @@ class TestFileJpeg(PillowTestCase): self.assertEqual(im.info.get("dpi"), (72, 72)) def test_invalid_exif_x_resolution(self): - # When no x or y resolution defined in EXIF + # When no x or y resolution is defined in EXIF im = Image.open("Tests/images/invalid-exif-without-x-resolution.jpg") # This should return the default, and not a ValueError or - # OSError for unidentified image. + # OSError for an unidentified image. self.assertEqual(im.info.get("dpi"), (72, 72)) def test_ifd_offset_exif(self): diff --git a/src/PIL/JpegImagePlugin.py b/src/PIL/JpegImagePlugin.py index 00ea95e41..193ca4bd0 100644 --- a/src/PIL/JpegImagePlugin.py +++ b/src/PIL/JpegImagePlugin.py @@ -172,7 +172,7 @@ def APP(self, marker): # 1 dpcm = 2.54 dpi dpi *= 2.54 self.info["dpi"] = int(dpi + 0.5), int(dpi + 0.5) - except (KeyError, TypeError, SyntaxError, ValueError, ZeroDivisionError): + except (KeyError, SyntaxError, ValueError, ZeroDivisionError): # SyntaxError for invalid/unreadable EXIF # KeyError for dpi not included # ZeroDivisionError for invalid dpi rational value