I’m trying to write a python function to parse the width and height from a jpeg file. The code I currently have looks like this
import struct
image = open('images/image.jpg','rb')
image.seek(199)
#reverse hex to deal with endianness...
hex = image.read(2)[::-1]+image.read(2)[::-1]
print(struct.unpack('HH',hex))
image.close()
There are a couple of problems with this though, firstly I need to look through the file to work out where to read from (after ff c0 00 11 08), and secondly I need to avoid picking up data from embedded thumbnails. Any suggestions?
The JPEG section of this function might be useful: http://code.google.com/p/bfg-pages/source/browse/trunk/pages/getimageinfo.py