exif.html
001| '''Title.'''
002|
003| from os import path
004| from PIL import Image
005|
006| ToInt = lambda bs: sum([b<<(bx*8) for bx,b in enumerate(reversed(bs))])
007|
008| ExifDataType = {
009| 1:('byte', 1, None),
010| 2:('char', 1, None),
011| 3:('short', 2, None),
012| 4:('long', 4, None),
013| 5:('rational', 8, None),
014| 7:('blob', 1, None),
015| 9:('slong', 4, None),
016| 10:('srational', 8, None)
017| }
018|
019|
020|
021| class ExifAttribute (object):
022| RecLen = 12
023| DType = lambda x: ExifDataType[x][0] if x in ExifDataType else ('%04x' % x)
024|
025| def __init__ (self, bs, ix):
026| self.ix = ix
027| self.tag = ToInt(bs[ix+0:ix+2])
028| self.typ = ToInt(bs[ix+2:ix+4])
029| self.nbr = ToInt(bs[ix+4:ix+8])
030| self.ofs = ToInt(bs[ix+8:ix+12])
031| self.des = '(unknown)'
032| self.ext = ''
033| self.val = -1
034|
035| def __str__ (self):
036| s = '%04x: [%04x] %s={%s} %s(%d) %08x'
037| v = ('%s (%s)' % (self.ext,self.val)) if self.ext else self.val
038| d = ExifAttribute.DType(self.typ)
039| t = (self.ix, self.tag, self.des, v, d,self.nbr, self.ofs)
040| return s % t
041|
042|
043| class ExifInformation (object):
044| def __init__ (self, bs):
045| self.hdr = bs[0:4].decode('utf-8')
046| self.nul = sum(bs[4:6])
047| self.bs = bs[6:]
048| self.bom = str(self.bs[0:2], encoding='utf-8')
049| self.f42 = ToInt(self.bs[2:4])
050| self.ifd = ToInt(self.bs[4:8])
051| self.noi = ToInt(self.bs[8:10])
052| self.ofs = 10
053| self.attrs = {}
054| self.ifd_ptr = None
055| self.gps_ifd = None
056| self.interop = None
057| assert self.nul == 0, RuntimeError('Unexpected EXIF Value: NUL=%04x (0000 was expected)' % self.nul)
058| assert self.f42 == 42, RuntimeError('Unexpected EXIF Value: F42=%04x (002a was expected)' % self.f42)
059| assert self.ifd == 8, RuntimeError('Unexpected EXIF Value: IFD=%04x (0008 was expected)' % self.ifd)
060| for _ in range(self.noi):
061| attr = ExifAttribute(self.bs, self.ofs)
062| method_name = 'attr_%04x' % attr.tag
063| method = self.__getattribute__(method_name)
064| if method: method(attr)
065| self.attrs[attr.tag] = attr
066| self.ofs += ExifAttribute.RecLen
067|
068| if self.ifd_ptr:
069| self.ofs = self.ifd_ptr.ofs
070| nbr_items = ToInt(self.bs[self.ofs:2+self.ofs])
071| self.ofs += 2
072| for _ in range(nbr_items):
073| attr = ExifAttribute(self.bs, self.ofs)
074| method_name = 'attr_%04x' % attr.tag
075| method = self.__getattribute__(method_name)
076| if method: method(attr)
077| self.attrs[attr.tag] = attr
078| self.ofs += ExifAttribute.RecLen
079|
080|
081| def __len__ (self): return len(self.bs)
082| def __getitem__ (self, ix): return self.bs[ix]
083| def __iter__ (self): return iter(self.bs)
084| def __str__ (self): return '[EXIF] %d items (%d bytes)' % (self.noi,len(self))
085|
086| def attr_0100 (self, attr):
087| attr.des = 'Image Width'
088| attr.val = int(attr.ofs >> 16)
089| def attr_0101 (self, attr):
090| attr.des = 'Image Height'
091| attr.val = int(attr.ofs >> 16)
092| def attr_0102 (self, attr):
093| attr.des = 'Bits/Sample'
094| ptr = attr.ofs
095| d1 = ToInt(self.bs[ptr+0:ptr+2])
096| d2 = ToInt(self.bs[ptr+2:ptr+4])
097| d3 = ToInt(self.bs[ptr+4:ptr+6])
098| attr.val = (d1, d2, d3)
099| def attr_0103 (self, attr):
100| exts = {1:'No compression', 6:'JPEG compression'}
101| attr.des = 'Compression'
102| attr.val = int(attr.ofs >> 16)
103| attr.ext = exts[attr.val] if attr.val in exts else '(unknown)'
104| def attr_0106 (self, attr):
105| exts = {2:'RGB', 6:'YCbCr'}
106| attr.des = 'Pixel Comp'
107| attr.val = int(attr.ofs >> 16)
108| attr.ext = exts[attr.val] if attr.val in exts else '(unknown)'
109| def attr_010e (self, attr):
110| attr.des = 'Image Descrip'
111| ptr = attr.ofs
112| siz = attr.nbr-1
113| attr.val = str(self.bs[ptr:ptr+siz], encoding='utf-8')
114| def attr_010f (self, attr):
115| attr.des = 'Make'
116| ptr = attr.ofs
117| siz = attr.nbr-1
118| attr.val = str(self.bs[ptr:ptr+siz], encoding='utf-8')
119| def attr_0110 (self, attr):
120| attr.des = 'Model'
121| ptr = attr.ofs
122| siz = attr.nbr-1
123| attr.val = str(self.bs[ptr:ptr+siz], encoding='utf-8')
124| def attr_0111 (self, attr):
125| attr.des = 'Strip Offsets'
126| def attr_0112 (self, attr):
127| exts = {1:'TL', 2:'TR', 3:'BR', 4:'BL', 5:'LT', 6:'RT', 7:'RB', 8:'RB'}
128| attr.des = 'Orientation'
129| attr.val = int(attr.ofs >> 16)
130| attr.ext = exts[attr.val] if attr.val in exts else '(unknown)'
131| def attr_0115 (self, attr):
132| attr.des = 'Samples/Pixel'
133| attr.val = int(attr.ofs >> 16)
134| def attr_0116 (self, attr):
135| attr.des = 'Rows/Strip'
136| attr.val = int(attr.ofs >> 16) if attr.typ == 3 else attr.ofs
137| def attr_0117 (self, attr):
138| attr.des = 'Strip Byte Count'
139| def attr_011a (self, attr):
140| attr.des = 'X Resolution'
141| ptr = attr.ofs
142| d1 = ToInt(self.bs[ptr+0:ptr+4])
143| d2 = ToInt(self.bs[ptr+4:ptr+8])
144| attr.val = float(d1) / float(d2)
145| def attr_011b (self, attr):
146| attr.des = 'Y Resolution'
147| ptr = attr.ofs
148| d1 = ToInt(self.bs[ptr+0:ptr+4])
149| d2 = ToInt(self.bs[ptr+4:ptr+8])
150| attr.val = float(d1) / float(d2)
151| def attr_011c (self, attr):
152| exts = {1:'Chunky', 2:'Planar'}
153| attr.des = 'Planar Config'
154| attr.val = int(attr.ofs >> 16)
155| attr.ext = exts[attr.val] if attr.val in exts else '(unknown)'
156| def attr_0128 (self, attr):
157| exts = {2:'inches', 3:'centimeters'}
158| attr.des = 'Resolution Unit'
159| attr.val = int(attr.ofs >> 16)
160| attr.ext = exts[attr.val] if attr.val in exts else '(unknown)'
161| def attr_012d (self, attr):
162| attr.des = 'Transfer Function'
163| def attr_0131 (self, attr):
164| attr.des = 'Software'
165| ptr = attr.ofs
166| siz = attr.nbr-1
167| attr.val = self.bs[ptr:ptr+siz].decode('utf-8')
168| def attr_0132 (self, attr):
169| attr.des = 'Date & Time'
170| ptr = attr.ofs
171| attr.val = self.bs[ptr:ptr+19].decode('utf-8')
172| def attr_013b (self, attr):
173| attr.des = 'Artist'
174| ptr = attr.ofs
175| siz = attr.nbr-1
176| attr.val = self.bs[ptr:ptr+siz].decode('utf-8')
177| def attr_013c (self, attr):
178| attr.des = 'Host Computer'
179| ptr = attr.ofs
180| siz = attr.nbr-1
181| attr.val = self.bs[ptr:ptr+siz].decode('utf-8')
182| def attr_013e (self, attr):
183| attr.des = 'White Point'
184| ptr = attr.ofs
185| d1 = ToInt(self.bs[ptr+0:ptr+4])
186| d2 = ToInt(self.bs[ptr+4:ptr+8])
187| d3 = ToInt(self.bs[ptr+8:ptr+12])
188| d4 = ToInt(self.bs[ptr+12:ptr+16])
189| attr.val = (float(d1)/float(d2), float(d3)/float(d4))
190| def attr_013f (self, attr):
191| attr.des = 'Primary Chromas'
192| ptr = attr.ofs
193| d1 = ToInt(self.bs[ptr+0:ptr+4])
194| d2 = ToInt(self.bs[ptr+4:ptr+8])
195| d3 = ToInt(self.bs[ptr+8:ptr+12])
196| d4 = ToInt(self.bs[ptr+12:ptr+16])
197| d5 = ToInt(self.bs[ptr+12:ptr+16])
198| d6 = ToInt(self.bs[ptr+16:ptr+20])
199| d7 = ToInt(self.bs[ptr+20:ptr+24])
200| d8 = ToInt(self.bs[ptr+24:ptr+28])
201| d9 = ToInt(self.bs[ptr+28:ptr+32])
202| da = ToInt(self.bs[ptr+32:ptr+36])
203| db = ToInt(self.bs[ptr+36:ptr+40])
204| dc = ToInt(self.bs[ptr+40:ptr+44])
205| attr.val = (float(d1)/float(d2), float(d3)/float(d4), float(d5)/float(d6),
206| float(d7)/float(d8), float(d9)/float(da), float(db)/float(dc))
207|
208| def attr_0142 (self, attr):
209| attr.des = '(unknown)'
210| def attr_0143 (self, attr):
211| attr.des = '(unknown)'
212|
213| def attr_0201 (self, attr):
214| attr.des = 'JPEG Format'
215| attr.val = '%08x' % attr.ofs
216| def attr_0202 (self, attr):
217| attr.des = 'JPEG Format Len'
218| attr.val = attr.ofs
219| def attr_0211 (self, attr):
220| attr.des = 'YCbCr Coeff'
221| ptr = attr.ofs
222| d1 = ToInt(self.bs[ptr+0:ptr+4])
223| d2 = ToInt(self.bs[ptr+4:ptr+8])
224| d3 = ToInt(self.bs[ptr+8:ptr+12])
225| d4 = ToInt(self.bs[ptr+12:ptr+16])
226| d5 = ToInt(self.bs[ptr+12:ptr+16])
227| d6 = ToInt(self.bs[ptr+16:ptr+20])
228| attr.val = (float(d1)/float(d2), float(d3)/float(d4), float(d5)/float(d6))
229| def attr_0212 (self, attr):
230| attr.des = 'YCbCr Sub Samp'
231| def attr_0213 (self, attr):
232| exts = {1:'centered', 2:'co-sited'}
233| attr.des = 'YCbCr Position'
234| attr.val = int(attr.ofs >> 16)
235| attr.ext = exts[attr.val] if attr.val in exts else '(unknown)'
236| def attr_0214 (self, attr):
237| attr.des = 'Reference B/W'
238| ptr = attr.ofs
239| d1 = ToInt(self.bs[ptr+0:ptr+4])
240| d2 = ToInt(self.bs[ptr+4:ptr+8])
241| d3 = ToInt(self.bs[ptr+8:ptr+12])
242| d4 = ToInt(self.bs[ptr+12:ptr+16])
243| d5 = ToInt(self.bs[ptr+12:ptr+16])
244| d6 = ToInt(self.bs[ptr+16:ptr+20])
245| d7 = ToInt(self.bs[ptr+20:ptr+24])
246| d8 = ToInt(self.bs[ptr+24:ptr+28])
247| d9 = ToInt(self.bs[ptr+28:ptr+32])
248| da = ToInt(self.bs[ptr+32:ptr+36])
249| db = ToInt(self.bs[ptr+36:ptr+40])
250| dc = ToInt(self.bs[ptr+40:ptr+44])
251| attr.val = (float(d1)/float(d2), float(d3)/float(d4), float(d5)/float(d6),
252| float(d7)/float(d8), float(d9)/float(da), float(db)/float(dc))
253|
254| def attr_8268 (self, attr):
255| attr.des = 'Copyright'
256| ptr = attr.ofs
257| siz = attr.nbr-1
258| attr.val = str(self.bs[ptr:ptr+siz], encoding='utf-8')
259| def attr_829a (self, attr):
260| attr.des = 'Exposure Time'
261| ptr = attr.ofs
262| d1 = ToInt(self.bs[ptr+0:ptr+4])
263| d2 = ToInt(self.bs[ptr+4:ptr+8])
264| attr.val = float(d1) / float(d2)
265| def attr_829d (self, attr):
266| attr.des = 'F-Stop Number'
267| ptr = attr.ofs
268| d1 = ToInt(self.bs[ptr+0:ptr+4])
269| d2 = ToInt(self.bs[ptr+4:ptr+8])
270| attr.val = float(d1) / float(d2)
271| def attr_8769 (self, attr):
272| attr.des = 'Exif IFD Pointer'
273| attr.val = '%08x' % attr.ofs
274| self.ifd_ptr = attr
275| def attr_8822 (self, attr):
276| attr.des = 'Exposure Program'
277| attr.val = int(attr.ofs >> 16)
278| def attr_8825 (self, attr):
279| attr.des = 'GPS IFD'
280| attr.val = '%08x' % attr.ofs
281| self.gps_ifd = attr
282| def attr_8827 (self, attr):
283| attr.des = 'ISO Speed Rating'
284| attr.val = int(attr.ofs >> 16)
285|
286| def attr_9000 (self, attr):
287| attr.des = 'EXIF Version'
288| v1 = chr(attr.ofs & 0x0ff)
289| v2 = chr((attr.ofs >> 8) & 0x0ff)
290| v3 = chr((attr.ofs >> 16) & 0x0ff)
291| v4 = chr((attr.ofs >> 24) & 0x0ff)
292| attr.val = '%s.%s.%s.%s' % (v1,v2,v3,v4)
293| def attr_9003 (self, attr):
294| attr.des = 'Orig Date & Time'
295| ptr = attr.ofs
296| attr.val = self.bs[ptr:ptr+19].decode('utf-8')
297| def attr_9004 (self, attr):
298| attr.des = 'Digitizing Date & Time'
299| ptr = attr.ofs
300| attr.val = self.bs[ptr:ptr+19].decode('utf-8')
301| def attr_9010 (self, attr):
302| attr.des = 'Modify Date Time Zone'
303| ptr = attr.ofs
304| siz = attr.nbr-1
305| attr.val = self.bs[ptr:ptr+siz].decode('utf-8')
306| def attr_9011 (self, attr):
307| attr.des = 'Original Date Time Zone'
308| ptr = attr.ofs
309| siz = attr.nbr-1
310| attr.val = self.bs[ptr:ptr+siz].decode('utf-8')
311| def attr_9012 (self, attr):
312| attr.des = 'Create Date Time Zone'
313| ptr = attr.ofs
314| siz = attr.nbr-1
315| attr.val = self.bs[ptr:ptr+siz].decode('utf-8')
316| def attr_9101 (self, attr):
317| attr.des = 'Component Config'
318| c1 = (attr.ofs & 0x0ff)
319| c2 = ((attr.ofs >> 8) & 0x0ff)
320| c3 = ((attr.ofs >> 16) & 0x0ff)
321| c4 = ((attr.ofs >> 24) & 0x0ff)
322| attr.val = '%d.%d.%d.%d' % (c1,c2,c3,c4)
323| def attr_9201 (self, attr):
324| attr.des = 'Shutter Speed'
325| def attr_9202 (self, attr):
326| attr.des = 'Lens Aperture'
327| def attr_9203 (self, attr):
328| attr.des = 'Brightness Value'
329| def attr_9204 (self, attr):
330| attr.des = 'Exposure Bias'
331| def attr_9205 (self, attr):
332| attr.des = 'Max Aperture'
333| def attr_9206 (self, attr):
334| attr.des = 'Subject Distance'
335| def attr_9207 (self, attr):
336| attr.des = 'Metering Mode'
337| def attr_9208 (self, attr):
338| attr.des = 'Light Source'
339| def attr_9209 (self, attr):
340| attr.des = 'Flash'
341| def attr_920a (self, attr):
342| attr.des = 'Lens Focal Length'
343| def attr_9214 (self, attr):
344| attr.des = 'Subject Area'
345| def attr_927c (self, attr):
346| attr.des = 'Manufacturer Notes'
347| ptr = attr.ofs
348| siz = attr.nbr-1
349| attr.val = self.bs[ptr:ptr+siz]
350| def attr_9286 (self, attr):
351| attr.des = 'User Comments'
352| ptr = attr.ofs
353| siz = attr.nbr-1
354| attr.val = self.bs[ptr:ptr+siz]
355| def attr_9290 (self, attr):
356| attr.des = 'DateTime subseconds'
357| s1 = chr((attr.ofs >> 8) & 0x0ff)
358| s2 = chr((attr.ofs >> 16) & 0x0ff)
359| s3 = chr((attr.ofs >> 24) & 0x0ff)
360| attr.val = '%s.%s.%s' % (s1,s2,s3)
361| def attr_9291 (self, attr):
362| attr.des = 'Orig DateTime subseconds'
363| s1 = chr((attr.ofs >> 8) & 0x0ff)
364| s2 = chr((attr.ofs >> 16) & 0x0ff)
365| s3 = chr((attr.ofs >> 24) & 0x0ff)
366| attr.val = '%s.%s.%s' % (s1,s2,s3)
367| def attr_9292 (self, attr):
368| attr.des = 'Digitized DateTime subseconds'
369| s1 = chr((attr.ofs >> 8) & 0x0ff)
370| s2 = chr((attr.ofs >> 16) & 0x0ff)
371| s3 = chr((attr.ofs >> 24) & 0x0ff)
372| attr.val = '%s.%s.%s' % (s1,s2,s3)
373|
374| def attr_a000 (self, attr):
375| attr.des = 'Flashpix Version'
376| v1 = chr(attr.ofs & 0x0ff)
377| v2 = chr((attr.ofs >> 8) & 0x0ff)
378| v3 = chr((attr.ofs >> 16) & 0x0ff)
379| v4 = chr((attr.ofs >> 24) & 0x0ff)
380| attr.val = '%s.%s.%s.%s' % (v1,v2,v3,v4)
381| def attr_a001 (self, attr):
382| exts = {1:'sRBG', 0x0ffff:'uncalibrated'}
383| attr.des = 'Color Space'
384| attr.val = int(attr.ofs >> 16)
385| attr.ext = exts[attr.val] if attr.val in exts else '(unknown)'
386| def attr_a002 (self, attr):
387| attr.des = 'Pixel X Dimension'
388| attr.val = attr.ofs
389| def attr_a003 (self, attr):
390| attr.des = 'Pixel Y Dimension'
391| attr.val = attr.ofs
392| def attr_a004 (self, attr):
393| attr.des = 'Related Audio File'
394| def attr_a005 (self, attr):
395| attr.des = 'interoperability'
396| self.interop = attr
397| def attr_a217 (self, attr):
398| attr.des = 'Sensing Method'
399| def attr_a301 (self, attr):
400| attr.des = 'Scene Type'
401| def attr_a401 (self, attr):
402| attr.des = 'Custom Rendered'
403| attr.val = int(attr.ofs >> 16)
404| def attr_a402 (self, attr):
405| attr.des = 'Exposure Mode'
406| def attr_a403 (self, attr):
407| attr.des = 'White Balance'
408| def attr_a404 (self, attr):
409| attr.des = 'Digital Zoom'
410| def attr_a405 (self, attr):
411| attr.des = '35mm Focal Length'
412| def attr_a406 (self, attr):
413| attr.des = 'Scene Capture Type'
414| def attr_a432 (self, attr):
415| attr.des = '(unknown)'
416| def attr_a433 (self, attr):
417| attr.des = '(unknown)'
418| def attr_a434 (self, attr):
419| attr.des = '(unknown)'
420| def attr_a460 (self, attr):
421| attr.des = '(unknown)'
422| def attr_ea1d (self, attr):
423| attr.des = '(unknown)'
424|
425| def hex_dump (self, fp):
426| row_size = 16
427| while self.ofs < len(self):
428| bs = self[self.ofs:self.ofs+row_size]
429| xs = ' '.join([('%02x' % b) for b in bs])
430| cs = ''.join([(chr(b) if 32 <= b < 127 else '.') for b in bs])
431| print('%05x: %-*s | %s' % (self.ofs, 3*row_size, xs, cs), file=fp)
432| self.ofs += row_size
433|
434|
435|
436| def exif_data (filename, datapath):
437| imgname = path.join(datapath, filename)
438| im = Image.open(imgname)
439| dpi = im.info['dpi']
440| inf = im.info['exif'] if 'exif' in im.info else {}
441| if not len(inf):
442| print('NO EXIF')
443| return None
444| return ExifInformation(inf)
445|
446|
447|
448| def exif_data_dump (filename, datapath, listpath):
449| imgname = path.join(datapath, filename)
450| im = Image.open(imgname)
451| dpi = im.info['dpi']
452| inf = im.info['exif'] if 'exif' in im.info else {}
453|
454| fn = path.join(listpath, '%s.out'%(path.splitext(filename)[0]))
455| fp = open(fn, mode='w', encoding='utf-8')
456| try:
457| print(filename, file=fp)
458| print('--', file=fp)
459| print('dpi: %d, %d' % dpi, file=fp)
460| print('--', file=fp)
461| if len(inf):
462| exif = ExifInformation(inf)
463| print(exif, file=fp)
464| print('exif-hdr: %s' % exif.hdr, file=fp)
465| print('exif-nul: %04x' % exif.nul, file=fp)
466| print('exif-bom: %s' % exif.bom, file=fp)
467| print('exif-42: %d' % exif.f42, file=fp)
468| print('exif-noi: %d' % exif.noi, file=fp)
469| print('exif-ifd: %08x' % exif.ifd, file=fp)
470| print('exif-ptr: %08x' % (exif.ifd_ptr.ofs if exif.ifd_ptr else 0), file=fp)
471| print('exif-gps: %08x' % (exif.gps_ifd.ofs if exif.gps_ifd else 0), file=fp)
472| print('exif-iop: %08x' % (exif.interop.ofs if exif.interop else 0), file=fp)
473| print(file=fp)
474|
475| for tag in sorted(exif.attrs):
476| print(exif.attrs[tag], file=fp)
477| else:
478| exif = None
479| print('### NO EXIF DATA ###', file=fp)
480| print('--', file=fp)
481| print(file=fp)
482| except:
483| raise
484| finally:
485| fp.close()
486| print('wrote: %s' % fn)
487|
488| return exif
489|
490|
491|
492| def exif_hex_dump (filename, datapath, listpath):
493| imgname = path.join(datapath, filename)
494| im = Image.open(imgname)
495| exif = im.info['exif'] if 'exif' in im.info else {}
496| dpi = im.info['dpi']
497| exif_hdr = exif[0:4].decode('utf-8')
498| exif_nul = sum(exif[4:6])
499| exif = exif[6:]
500|
501| fn = path.join(listpath, '%s.hex'%(path.splitext(filename)[0]))
502| fp = open(fn, mode='w', encoding='utf-8')
503| try:
504| print(filename, file=fp)
505| print('hdr: %s (%d)' % (exif_hdr,exif_nul), file=fp)
506| print('dpi: %d, %d' % dpi, file=fp)
507| print('exif: %d bytes' % len(exif), file=fp)
508| print(file=fp)
509| RowSize = 16
510| offset = 0
511| while offset < len(exif):
512| bs = exif[offset:offset+RowSize]
513| xs = ' '.join([('%02x' % b) for b in bs])
514| cs = ''.join([(chr(b) if 32 <= b < 127 else '.') for b in bs])
515| print('%05x: %-*s | %s' % (offset, 3*RowSize, xs, cs), file=fp)
516|
517| offset += RowSize
518| except:
519| raise
520| finally:
521| fp.close()
522|
523| return exif
524|
525|
526|
527| '''eof'''
528|