Merge pull request #47 from codecarson/st3

Update JPG dimension checking for Python3.
This commit is contained in:
Liam Cain
2013-11-27 10:00:17 -08:00

View File

@@ -35,27 +35,28 @@ def getImageInfo(data):
height = int(h)
# handle JPEGs
# elif (size >= 2) and data[:2] == b'\377\330':
# jpeg = io.StringIO(olddata)
# print(bytes(ord(jpeg.read(1))))
# jpeg.read(2)
# b = jpeg.read(1)
# try:
# while (b and ord(b) != 0xDA):
# while (ord(b) != 0xFF): b = jpeg.read(1)
# while (ord(b) == 0xFF): b = jpeg.read(1)
# if (ord(b) >= 0xC0 and ord(b) <= 0xC3):
# jpeg.read(3)
# h, w = struct.unpack(">HH", jpeg.read(4))
# break
# else:
# jpeg.read(int(struct.unpack(">H", jpeg.read(2))[0])-2)
# b = jpeg.read(1)
# width = int(w)
# height = int(h)
# except struct.error:
# pass
# except ValueError:
# pass
elif (size >= 2) and data[:2] == b'\377\330':
jpeg = io.BytesIO(data)
jpeg.read(2)
b = jpeg.read(1)
try:
while (b != b''):
while (b != b'\xFF'): b = jpeg.read(1)
while (b == b'\xFF'): b = jpeg.read(1)
if (b >= b'\xC0' and b <= b'\xC3'):
jpeg.read(3)
h, w = struct.unpack(">HH", jpeg.read(4))
break
else:
jpeg.read(int(struct.unpack(">H", jpeg.read(2))[0])-2)
b = jpeg.read(1)
width = int(w)
height = int(h)
except struct.error:
pass
except ValueError:
pass
return width, height