Browse Source

Refactor EXIF module.

debian-sid
Tom Payne 14 years ago
parent
commit
89ae650c2c
  1. 34
      igc2kmz/exif.py
  2. 2
      test/test_exif.py

34
igc2kmz/exif.py

@ -38,7 +38,7 @@ class SyntaxError(RuntimeError):
pass
class Tiff(object):
class TIFF(object):
def __init__(self, data):
self.data = data
@ -225,7 +225,7 @@ IFD_POINTER_IDS = {
def exif(data):
if data[0:6] != 'Exif\0\0':
raise SyntaxError, 'Unrecognised EXIF header %s' % repr(data[0:6])
tiff = Tiff(data[6:])
tiff = TIFF(data[6:])
result = {}
for ifd_offset in tiff.ifd_offsets():
for id, value in tiff.ifd_tags(ifd_offset):
@ -240,25 +240,27 @@ def exif(data):
SOI = 0xffd8
APP1 = 0xffe1
SOF = 0xffc0
SOS = 0xffda
class Jpeg(object):
class JPEG(object):
def __init__(self, file):
self.file = file
self.exif = {}
self.height = self.width = None
for id, data in JPEG.chunks(file):
if id == APP1:
self.exif = exif(data)
elif id == SOF:
self.height, self.width = struct.unpack('>HH', data[1:5])
def chunks(self):
if struct.unpack('>H', self.file.read(2)) != (SOI,):
@classmethod
def chunks(self, file):
if struct.unpack('>H', file.read(2)) != (SOI,):
raise SyntaxError
id, = struct.unpack('>H', self.file.read(2))
id, = struct.unpack('>H', file.read(2))
while id != SOS:
size, = struct.unpack('>H', self.file.read(2))
yield (id, self.file.read(size - 2))
id, = struct.unpack('>H', self.file.read(2))
def exif(self):
for id, data in self.chunks():
if id == APP1:
return exif(data)
return {}
size, = struct.unpack('>H', file.read(2))
yield (id, file.read(size - 2))
id, = struct.unpack('>H', file.read(2))

2
test/test_exif.py

@ -29,7 +29,7 @@ import igc2kmz.exif
def main(argv):
for arg in argv[1:]:
try:
pprint.pprint(igc2kmz.exif.Jpeg(open(arg)).exif())
pprint.pprint(igc2kmz.exif.JPEG(open(arg)).__dict__)
except igc2kmz.exif.SyntaxError, line:
print "%s: %s" % (arg, line)

Loading…
Cancel
Save