summaryrefslogtreecommitdiff
path: root/rapid/metadata.py
blob: 3077a61e55a850fe0e0f404a1a080ae077cfe7f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
#!/usr/bin/python
# -*- coding: latin1 -*-

### Copyright (C) 2007-10 Damon Lynch <damonlynch@gmail.com>

### This program is free software; you can redistribute it and/or modify
### it under the terms of the GNU General Public License as published by
### the Free Software Foundation; either version 2 of the License, or
### (at your option) any later version.

### This program is distributed in the hope that it will be useful,
### but WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
### GNU General Public License for more details.

### You should have received a copy of the GNU General Public License
### along with this program; if not, write to the Free Software
### Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import re
import datetime
import sys
import subprocess
import config
import types
import time

try:
    import pyexiv2
except ImportError:
    sys.stderr.write("You need to install pyexiv2, the python binding for exiv2, to run this program.\n" )
    sys.exit(1)
    
#only pyexiv2 <= 0.1.1 does not use the "Rational" class 
if 'Rational' in dir(pyexiv2):
    usesRational = True
else:
    usesRational = False

#get versions of pyexiv2 and exiv2 libraries
if 'version_info' in dir(pyexiv2):
    pyexiv2_version = pyexiv2.version_info
    exiv2_version = pyexiv2.exiv2_version_info
    baseclass = eval('pyexiv2.metadata.ImageMetadata')
else:
    pyexiv2_version = (0,1,'x')
    # try to determine the version of exiv2 from it's standard output
    try:
        proc = subprocess.Popen(['exiv2', '-V'], stdout=subprocess.PIPE)
        output = proc.communicate()[0]
    except:
        output = None
        exiv2_version = None
    if output:
        # assume output contains the line 'exiv2 0.x' or possibly
        # 'exiv2 0.x.x'
        start = output.find('exiv2 ')
        if start < 0:
            exiv2_version = None            
        else:
            end = output.find('\n', start)
            if end:
                exiv2_v = output[6:end]
            else:
                exiv2_v = output[6:]
            
            exiv2_version = []
            dot = exiv2_v.find('.')
            while dot > 0:
                exiv2_version += [int(exiv2_v[:dot])]
                exiv2_v = exiv2_v[dot+1:]
                dot = exiv2_v.find('.')
            exiv2_version += [int(exiv2_v)]
            exiv2_version = tuple(exiv2_version) 
            
        
    baseclass = eval('pyexiv2.Image')

def __version_info(version):
    if not version:
        return ''
    else:
        v = ''
        for i in version:
            v += '.%s' % i
        return v[1:]    
    
def version_info():
    return __version_info(pyexiv2_version)
    
def exiv2_version_info():
    return __version_info(exiv2_version)    

RAW_FILE_EXTENSIONS = ['arw', 'dcr', 'cr2', 'crw',  'dng', 'mos', 'mrw', 
                        'nef', 'orf', 'pef', 'raf', 'raw', 'sr2']

#exiv2 0.18.1 introduces support for Panasonic .RW2 files
#pyexiv2 in combination with exiv2 0.18 segfaults when trying to read an
#RW2 files, so we should not read those! exiv2 0.17 & pyexiv2 segfaults
#with MEF files.

if exiv2_version is not None:
    if exiv2_version[0] > 0:
        RAW_FILE_EXTENSIONS += ['rw2', 'mef']
    else:
        if exiv2_version[1] > 17:
            RAW_FILE_EXTENSIONS += ['mef']
        if exiv2_version[1] > 18:
            RAW_FILE_EXTENSIONS += ['rw2']
        else:
            if len(exiv2_version) > 2:
                if exiv2_version[2] >= 1:
                    RAW_FILE_EXTENSIONS += ['rw2']
                
RAW_FILE_EXTENSIONS.sort()

NON_RAW_IMAGE_FILE_EXTENSIONS = ['jpg', 'jpe', 'jpeg', 'tif', 'tiff']


class MetaData(baseclass):
    """
    Class providing human readable access to image metadata

    """
    
    __version01__ = pyexiv2_version[0] == 0 and pyexiv2_version[1] == 1

    def aperture(self, missing=''):
        """ 
        Returns in string format the floating point value of the image's aperture.
        
        Returns missing if the metadata value is not present.
        """
        
        try:
            if  usesRational:
                a = self["Exif.Photo.FNumber"]
                a0,  a1 = str(a).split('/')
            else:
                a0, a1 = self["Exif.Photo.FNumber"]
            a = float(a0) / float(a1)
            return "%.1f" % a
        except:
            return missing
            
    def iso(self, missing=''):
        """ 
        Returns in string format the integer value of the image's ISO.
        
        Returns missing if the metadata value is not present.
        """
        try:
            return "%s" % (self["Exif.Photo.ISOSpeedRatings"])
        except:
            return missing
            
    def exposureTime(self, alternativeFormat=False, missing=''):
        """ 
        Returns in string format the exposure time of the image.
        
        Returns missing if the metadata value is not present.
        
        alternativeFormat is useful if the value is going to be  used in a 
        purpose where / is an invalid character, e.g. file system names.  
        
        alternativeFormat is False:
        For exposures less than one second, the result is formatted as a 
        fraction e.g. 1/125
        For exposures greater than or equal to one second, the value is 
        formatted as an integer e.g. 30
        
        alternativeFormat is True:
        For exposures less than one second, the result is formatted as an 
        integer e.g. 125
        For exposures less than one second but more than or equal to 
        one tenth of a second, the result is formatted as an integer 
        e.g. 3 representing 3/10 of a second
        For exposures greater than or equal to one second, the value is 
        formatted as an integer with a trailing s e.g. 30s
        """

        try:
            if usesRational:

                e = str(self["Exif.Photo.ExposureTime"])

                e0,  e1 = e.split('/')
                e0 = int(e0)
                e1 = int(e1)
                # some values, e.g. Nikon, are in the format "10/1600"
                if (e0 > 1) and (e0 < e1):
                    e1 = e1 / e0
                    e0 = 1
            else:
                e0, e1 = self["Exif.Photo.ExposureTime"]
            
            if e1 > e0:
                if alternativeFormat:
                    if e0 == 1:
                        return str(e1)
                    else:
                        return  str(e0)
                else:
                    return "%s/%s" % (e0,e1)
            elif e0 > e1:
                e = float(e0) / e1
                if alternativeFormat:
                    return "%.0fs" % e
                else:
                    return "%.0f" % e
            else:
                    return "1s"
        except:
            return missing
        
    def focalLength(self, missing=''):
        """ 
        Returns in string format the focal length of the lens used to record the image.
        
        Returns missing if the metadata value is not present.
        """
        try:
            if usesRational:
                f = str(self["Exif.Photo.FocalLength"])
                f0,  f1 = f.split('/')
            else:
                f0, f1 = self["Exif.Photo.FocalLength"]
                
            f0 = float(f0)
            if not f1:
                f1 = 1.0
            else:
                f1 = float(f1)

            return "%.0f" % (f0 / f1)
        except:
            return missing
            
            
    def cameraMake(self, missing=''):
        """ 
        Returns in string format the camera make (manufacturer) used to record the image.
        
        Returns missing if the metadata value is not present.
        """
        try:
            return self["Exif.Image.Make"].strip()
        except:
            return missing
    
    def cameraModel(self, missing=''):
        """ 
        Returns in string format the camera model used to record the image.
        
        Returns missing if the metadata value is not present.
        """
        try:
            return self["Exif.Image.Model"].strip()
        except:
            return missing
            
    def cameraSerial(self,  missing=''):
        try:
            keys = self.rpd_keys()
            if 'Exif.Canon.SerialNumber' in keys:
                v = self['Exif.Canon.SerialNumber']
            elif 'Exif.Nikon3.SerialNumber' in keys:
                v = self['Exif.Nikon3.SerialNumber']
            elif 'Exif.OlympusEq.SerialNumber' in keys:
                v = self['Exif.OlympusEq.SerialNumber']
            elif 'Exif.Olympus.SerialNumber' in keys:
                v = self['Exif.Olympus.SerialNumber']
            elif 'Exif.Olympus.SerialNumber2' in keys:
                v = self['Exif.Olympus.SerialNumber2']
            elif 'Exif.Panasonic.SerialNumber' in keys:
                v = self['Exif.Panasonic.SerialNumber']
            elif 'Exif.Fujifilm.SerialNumber' in keys:
                v = self['Exif.Fujifilm.SerialNumber']
            elif 'Exif.Image.CameraSerialNumber' in keys:
                v = self['Exif.Image.CameraSerialNumber']
            else:
                return missing
            return str(v)
        except:
            return missing
            
    def shutterCount(self,  missing=''):
        try:
            keys = self.rpd_keys()
            if 'Exif.Nikon3.ShutterCount' in keys:
                v = self['Exif.Nikon3.ShutterCount']
            elif 'Exif.Canon.FileNumber' in keys:
                v = self['Exif.Canon.FileNumber']
            elif 'Exif.Canon.ImageNumber' in keys:
                v = self['Exif.Canon.ImageNumber']
            else:
                return missing
            return str(v)
        except:
            return missing
            
    def ownerName(self,  missing=''):
        """ returns camera name recorded by select Canon cameras"""
        try:
            return self['Exif.Canon.OwnerName'].strip()
        except:
            return missing
            
    def shortCameraModel(self, includeCharacters = '', missing=''):
        """ 
        Returns in shorterned string format the camera model used to record the image.
        
        Returns missing if the metadata value is not present.
        
        The short format is determined by the first occurrence of a digit in the 
        camera model, including all alphaNumeric characters before and after 
        that digit up till a non-alphanumeric character, but with these interventions:
        
        1. Canon "Mark" designations are shortened prior to conversion.
        2. Names like "Canon EOS DIGITAL REBEL XSi" do not have a number and must
            and treated differently (see below)
        
        Examples:
        Canon EOS 300D DIGITAL -> 300D
        Canon EOS 5D -> 5D
        Canon EOS 5D Mark II -> 5DMkII
        NIKON D2X -> D2X
        NIKON D70 -> D70
        X100,D540Z,C310Z -> X100
        Canon EOS DIGITAL REBEL XSi -> XSi
        Canon EOS Digital Rebel XS -> XS
        Canon EOS Digital Rebel XTi -> XTi
        Canon EOS Kiss Digital X -> Digital
        Canon EOS Digital Rebel XT -> XT
        EOS Kiss Digital -> Digital        
        Canon Digital IXUS Wireless -> Wireless
        Canon Digital IXUS i zoom -> zoom
        Canon EOS Kiss Digital N -> N
        Canon Digital IXUS IIs -> IIs
        IXY Digital L -> L
        Digital IXUS i -> i
        IXY Digital -> Digital
        Digital IXUS -> IXUS
        
        The optional includeCharacters allows additional characters to appear 
        before and after the digits. 
        Note: special includeCharacters MUST be escaped as per syntax of a 
        regular expressions (see documentation for module re)
       
        Examples:
        
        includeCharacters = '':
        DSC-P92 -> P92 
        includeCharacters = '\-':
        DSC-P92 -> DSC-P92 
        
        If a digit is not found in the camera model, the last word is returned.
        
        Note: assume exif values are in ENGLISH, regardless of current platform
        """
        m = self.cameraModel()
        m = m.replace(' Mark ', 'Mk') 
        if m:
            s = r"(?:[^a-zA-Z0-9%s]?)(?P<model>[a-zA-Z0-9%s]*\d+[a-zA-Z0-9%s]*)"\
                % (includeCharacters, includeCharacters, includeCharacters)
            r = re.search(s, m)
            if r:
                return r.group("model")
            else:
                head,  space,  model = m.strip().rpartition(' ')
                return model
        else:
            return missing
    
    def filterMangledDates(self, d):
        """
        Some EXIF dates are badly formed. Try to fix them
        """
        
        _datetime = d.strip()
        # remove any weird characters at the end of the string
        while _datetime and not _datetime[-1].isdigit():
            _datetime = _datetime[:-1]
        _date,  _time = _datetime.split(' ')
        _datetime = "%s %s" % (_date.replace(":",  "-") ,  _time.replace("-",  ":"))
        try:
            d = datetime.datetime.strptime(_datetime, '%Y-%m-%d %H:%M:%S')
        except:
            d = None
        return d
                
    def dateTime(self, missing=''):
        """ 
        Returns in python datetime format the date and time the image was 
        recorded.
        
        Trys to get value from exif key "Exif.Photo.DateTimeOriginal".
        If that does not exist, trys key "Exif.Image.DateTime"
        
        Returns missing either metadata value is not present.
        """
        keys = self.rpd_keys()
        try:
            if "Exif.Photo.DateTimeOriginal" in keys:
                v = self["Exif.Photo.DateTimeOriginal"]
            else:
                v = self["Exif.Image.DateTime"]
            if isinstance(v, types.StringType):
                v = self.filterMangledDates(v)
                if v is None:
                    v = missing
            return v
        except:
            return missing
            
    def timeStamp(self, missing=''):
        dt = self.dateTime(missing=None)
        if not dt is None:
            try:
                t = dt.timetuple()
                ts = time.mktime(t)
            except:
                ts = missing
        else:
            ts = missing
        return ts
            
    def subSeconds(self,  missing='00'):
        """ returns the subsecond the image was taken, as recorded by the camera"""
        try:
            return str(self["Exif.Photo.SubSecTimeOriginal"])
        except:
            return missing
            
    def orientation(self, missing=''):
        """
        Returns the orientation of the image, as recorded by the camera
        Return type int
        """
        try:
            v = self['Exif.Image.Orientation']
            if isinstance(v, types.StringType):
                # pyexiv2 >= 0.2 returns a string, not an int
                v = int(v)
            return v
        except:
            return missing
            
    # following class methods are designed to cope with using both
    # pyexiv2 0.1.x and pyexiv2 0.2.x
            
    def getThumbnailData(self, max_size_needed=0):
        """
        Returns a thumbnail of the image.
        
        If the image supports multiple thumbnails, and max_size_needed
        is not 0, then it will search for the smallest thumbnail that 
        matches the size required 
        
        The image will be in whatever format the thumbnail itself is, 
        typically a jpeg or tiff.
        """
        if self.__version01__:
            return pyexiv2.Image.getThumbnailData(self)[1]

        else:
            if not self.previews:
                return None, None
            else:
                if max_size_needed:
                    for thumbnail in self.previews:
                        if thumbnail.dimensions[0] >= max_size_needed or thumbnail.dimensions[1] >= max_size_needed:
                            break
                else:
                    thumbnail = self.previews[-1]
                        
                return thumbnail.data
                
    def read(self):
        if self.__version01__:
            self.readMetadata()
        else:
            pyexiv2.metadata.ImageMetadata.read(self)
            
    def rpd_keys(self):
        if self.__version01__:
            return pyexiv2.Image.exifKeys(self)
        else:
            return self.exif_keys
            
    def __getitem__(self, key):
        if self.__version01__:
            v = pyexiv2.Image.__getitem__(self, key)
        else:
            v = pyexiv2.metadata.ImageMetadata.__getitem__(self, key).raw_value
        # strip out null bytes from strings
        if isinstance(v, types.StringType):
            v = v.replace('\x00', '')
        return v
        

class DummyMetaData(MetaData):
    """
    Class which gives metadata values for an imaginary image.
    
    Useful for displaying in preference examples etc. when no image is ready to
    be downloaded.
    
    See MetaData class for documentation of class methods.
    """

    def __init__(self):
        pass
        
    def readMetadata(self):
        pass
        
    def aperture(self, missing=''):
        return "2.0"
            
    def iso(self, missing=''):
        return "100"
            
    def exposureTime(self, alternativeFormat=False, missing=''):
        if alternativeFormat:
            return  "4000"
        else:
            return  "1/4000"
        
    def focalLength(self, missing=''):
        return "135"
            
    def cameraMake(self, missing=''):
        return "Canon"
    
    def cameraModel(self, missing=''):
        return "Canon EOS 5D"
            
    def shortCameraModel(self, includeCharacters = '', missing=''):
        return "5D"
        
    def cameraSerial(self,  missing=''):
        return '730402168'
        
    def shutterCount(self,  missing=''):
        return '387'
        
    def ownerName(self,  missing=''):
        return 'Photographer Name'
        
    def dateTime(self, missing=''):
        return datetime.datetime.now()
        
    def subSeconds(self,  missing='00'):
        return '57'
        
    def orientation(self, missing=''):
        return 1
            
if __name__ == '__main__':
    import sys
    
    
    if (len(sys.argv) != 2):
        print 'Usage: ' + sys.argv[0] + ' path/to/photo/containing/metadata'
        m = DummyMetaData()

    else:
        m = MetaData(sys.argv[1])
        m.read()
        
    print "f"+ m.aperture('missing ')
    print "ISO " + m.iso('missing ')
    print m.exposureTime(missing='missing ') + " sec"
    print m.exposureTime(alternativeFormat=True,  missing='missing ')
    print m.focalLength('missing ') + "mm"
    print m.cameraMake()
    print m.cameraModel()
    print m.shortCameraModel()
    print m.shortCameraModel(includeCharacters = "\-")
    print m.dateTime()
    print m.orientation()
    print 'Serial number:',  m.cameraSerial()
    print 'Shutter count:', m.shutterCount()
    print 'Subseconds:',  m.subSeconds()