diff --git a/PIL/Jpeg2KImagePlugin.py b/PIL/Jpeg2KImagePlugin.py index 66069802e..0a7a6e297 100644 --- a/PIL/Jpeg2KImagePlugin.py +++ b/PIL/Jpeg2KImagePlugin.py @@ -172,18 +172,16 @@ class Jpeg2KImageFile(ImageFile.ImageFile): fd = -1 length = -1 - if hasattr(self.fp, "fileno"): + try: + fd = self.fp.fileno() + length = os.fstat(fd).st_size + except: + fd = -1 try: - fd = self.fp.fileno() - length = os.fstat(fd).st_size - except: - fd = -1 - elif hasattr(self.fp, "seek"): - try: - pos = f.tell() - f.seek(0, 2) - length = f.tell() - f.seek(pos, 0) + pos = self.fp.tell() + self.fp.seek(0, 2) + length = self.fp.tell() + self.fp.seek(pos, 0) except: length = -1 diff --git a/Tests/test_file_jpeg2k.py b/Tests/test_file_jpeg2k.py index b763687f7..e115df3a8 100644 --- a/Tests/test_file_jpeg2k.py +++ b/Tests/test_file_jpeg2k.py @@ -39,6 +39,14 @@ class TestFileJpeg2k(PillowTestCase): self.assertEqual(im.size, (640, 480)) self.assertEqual(im.format, 'JPEG2000') + def test_bytesio(self): + with open('Tests/images/test-card-lossless.jp2', 'rb') as f: + data = BytesIO(f.read()) + im = Image.open(data) + im.load() + print ("bytesio") + self.assert_image_similar(im, test_card, 1.0e-3) + # These two test pre-written JPEG 2000 files that were not written with # PIL (they were made using Adobe Photoshop)