f doesn't exist, BytesIO objects have fileno(), but may return OsError

This commit is contained in:
wiredfool 2014-07-01 11:09:20 -07:00
parent a47b8c15da
commit cec5fd9d38
2 changed files with 17 additions and 11 deletions

View File

@ -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

View File

@ -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)