From cec5fd9d38aac5534e7e3b0078cbed98a4a6c0c6 Mon Sep 17 00:00:00 2001 From: wiredfool Date: Tue, 1 Jul 2014 11:09:20 -0700 Subject: [PATCH 1/2] f doesn't exist, BytesIO objects have fileno(), but may return OsError --- PIL/Jpeg2KImagePlugin.py | 20 +++++++++----------- Tests/test_file_jpeg2k.py | 8 ++++++++ 2 files changed, 17 insertions(+), 11 deletions(-) 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) From 19bf3390a91052938915a36bf9cca9f9853f824c Mon Sep 17 00:00:00 2001 From: wiredfool Date: Tue, 1 Jul 2014 12:53:15 -0700 Subject: [PATCH 2/2] Removed extra print --- Tests/test_file_jpeg2k.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Tests/test_file_jpeg2k.py b/Tests/test_file_jpeg2k.py index e115df3a8..23564c434 100644 --- a/Tests/test_file_jpeg2k.py +++ b/Tests/test_file_jpeg2k.py @@ -44,7 +44,6 @@ class TestFileJpeg2k(PillowTestCase): 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