Skip to content

Commit

Permalink
Merge pull request #4240 from radarhere/mpo
Browse files Browse the repository at this point in the history
Raise a specific exception if no data is found for an MPO frame
  • Loading branch information
hugovk committed Nov 30, 2019
2 parents 1e73519 + 47b2ae9 commit a776255
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
Binary file added Tests/images/sugarshack_no_data.mpo
Binary file not shown.
7 changes: 7 additions & 0 deletions Tests/test_file_mpo.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ def test_mp_offset(self):
self.assertEqual(mpinfo[45056], b"0100")
self.assertEqual(mpinfo[45057], 2)

def test_mp_no_data(self):
# This image has been manually hexedited to have the second frame
# beyond the end of the file
with Image.open("Tests/images/sugarshack_no_data.mpo") as im:
with self.assertRaises(ValueError):
im.seek(1)

def test_mp_attribute(self):
for test_file in test_files:
with Image.open(test_file) as im:
Expand Down
5 changes: 4 additions & 1 deletion src/PIL/MpoImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ def seek(self, frame):
self.offset = self.__mpoffsets[frame]

self.fp.seek(self.offset + 2) # skip SOI marker
if i16(self.fp.read(2)) == 0xFFE1: # APP1
segment = self.fp.read(2)
if not segment:
raise ValueError("No data found for frame")
if i16(segment) == 0xFFE1: # APP1
n = i16(self.fp.read(2)) - 2
self.info["exif"] = ImageFile._safe_read(self.fp, n)

Expand Down

0 comments on commit a776255

Please sign in to comment.