- Python3 - . Acorn, , Python3:
import struct
import io
height = -1
width = -1
dafile = open('test.jpg', 'rb')
jpeg = io.BytesIO(dafile.read())
try:
type_check = jpeg.read(2)
if type_check != b'\xff\xd8':
print("Not a JPG")
else:
byte = jpeg.read(1)
while byte != b"":
while byte != b'\xff': byte = jpeg.read(1)
while byte == b'\xff': byte = jpeg.read(1)
if (byte >= b'\xC0' and byte <= 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)
byte = jpeg.read(1)
width = int(w)
height = int(h)
print("Width: %s, Height: %s" % (width, height))
finally:
jpeg.close()