From 832e11bed50d66e38e0bae5ac2ff8c9219388fc1 Mon Sep 17 00:00:00 2001 From: "Eric W. Brown" Date: Wed, 23 Jul 2014 11:27:46 -0400 Subject: [PATCH] Enabled seeking in MPO files. Note to self: either put the offset in the load_seek() definition or the tile definition, not both. --- PIL/MpoImagePlugin.py | 9 ++++++++- Tests/test_file_mpo.py | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/PIL/MpoImagePlugin.py b/PIL/MpoImagePlugin.py index 6594b74b0..6dda2e513 100644 --- a/PIL/MpoImagePlugin.py +++ b/PIL/MpoImagePlugin.py @@ -49,14 +49,21 @@ class MpoImageFile(JpegImagePlugin.JpegImageFile): self.__fp.seek(self.__mpoffsets[0]) # get ready to read first frame self.__frame = 0 self.offset = 0 + # for now we can only handle reading and individual frame extraction + self.readonly = 1 + + def load_seek(self, pos): + self.__fp.seek(pos) def seek(self, frame): if frame < 0 or frame >= self.__framecount: raise EOFError("no more images in MPO file") else: self.fp = self.__fp - self.fp.seek(self.__mpoffsets[frame]) self.offset = self.__mpoffsets[frame] + self.tile = [ + ("jpeg", (0, 0) + self.size, self.offset, (self.mode, "")) + ] self.__frame = frame def tell(self): diff --git a/Tests/test_file_mpo.py b/Tests/test_file_mpo.py index a0bcf6f40..b4ab38733 100644 --- a/Tests/test_file_mpo.py +++ b/Tests/test_file_mpo.py @@ -73,6 +73,20 @@ class TestFileMpo(PillowTestCase): # and this one, too im.seek(0) self.assertEqual(im.tell(), 0) + + def test_image_grab(self): + for test_file in test_files: + im = Image.open(test_file) + self.assertEqual(im.tell(), 0) + im0 = im.tobytes() + im.seek(1) + self.assertEqual(im.tell(), 1) + im1 = im.tobytes() + im.seek(0) + self.assertEqual(im.tell(), 0) + im0 = im.tobytes() + self.assertEqual(im0, im02) + self.assertNotEqual(im0, im1) if __name__ == '__main__':