-
Notifications
You must be signed in to change notification settings - Fork 0
/
extracttime.py
95 lines (87 loc) · 3.6 KB
/
extracttime.py
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
import cPickle
import sys
import collections
import time,math
import os
import struct
import imghdr
import exifread
def get_image_size(fname):
'''Determine the image type of fhandle and return its size.
from draco'''
fhandle = open(fname, 'rb')
head = fhandle.read(24)
tags = {}
if len(head) != 24:
return
if imghdr.what(fname) == 'png':
check = struct.unpack('>i', head[4:8])[0]
if check != 0x0d0a1a0a:
return
width, height = struct.unpack('>ii', head[16:24])
elif imghdr.what(fname) == 'gif':
width, height = struct.unpack('<HH', head[6:10])
elif imghdr.what(fname) == 'jpeg':
try:
fhandle.seek(0) # Read 0xff next
size = 2
ftype = 0
while not 0xc0 <= ftype <= 0xcf:
fhandle.seek(size, 1)
byte = fhandle.read(1)
while ord(byte) == 0xff:
byte = fhandle.read(1)
ftype = ord(byte)
size = struct.unpack('>H', fhandle.read(2))[0] - 2
# We are at a SOFn block
fhandle.seek(1, 1) # Skip `precision' byte.
height, width = struct.unpack('>HH', fhandle.read(4))
except Exception: #IGNORE:W0703
return
fhandle.seek(0,0);
tags = exifread.process_file(fhandle);
else:
return
return width, height,tags
#['EXIF MakerNote', 'GPS GPSLongitude', 'EXIF ApertureValue', 'Image ExifOffset', 'EXIF ComponentsConfiguration', 'EXIF FlashPixVersion', 'GPS GPSLatitude', 'Image DateTime', 'EXIF ShutterSpeedValue', 'EXIF ColorSpace', 'EXIF MeteringMode', 'EXIF ExifVersion', 'Image Software', 'Thumbnail ResolutionUnit', 'EXIF Flash', 'EXIF FocalLengthIn35mmFilm', 'Image Model', 'Image Orientation', 'EXIF DateTimeOriginal', 'Image YCbCrPositioning', 'Thumbnail JPEGInterchangeFormat', 'EXIF FNumber', 'EXIF ExifImageLength', 'EXIF SceneType', 'Image ResolutionUnit', 'Thumbnail XResolution', 'EXIF LensMake', 'Image GPSInfo', 'EXIF ExposureProgram', 'Thumbnail JPEGInterchangeFormatLength', 'EXIF ExposureMode', 'GPS GPSLatitudeRef', 'GPS GPSImgDirectionRef', 'GPS GPSImgDirection', 'EXIF ExifImageWidth', 'GPS GPSAltitudeRef', 'EXIF SceneCaptureType', 'JPEGThumbnail', 'GPS GPSTimeStamp', 'EXIF SubjectArea', 'EXIF SubSecTimeOriginal', 'EXIF BrightnessValue', 'EXIF LensModel', 'EXIF DateTimeDigitized', 'EXIF FocalLength', 'EXIF ExposureTime', 'Image XResolution', 'Image Make', 'EXIF WhiteBalance', 'GPS GPSAltitude', 'EXIF ISOSpeedRatings', 'Image YResolution', 'Thumbnail Compression', 'GPS GPSLongitudeRef', 'EXIF LensSpecification', 'EXIF SensingMethod', 'EXIF SubSecTimeDigitized', 'Thumbnail YResolution']
allowedext = set(".JPG,.jpg".split(","))
class Scanner:
def __init__(self):
self.names = collections.defaultdict(list)
self.paths = {}
self.sizes = collections.defaultdict(list)
def scan(self,path,basepath):
for p in os.listdir(path):
fp = os.path.join(path,p)
bfp = os.path.join(basepath,p)
e = os.path.splitext(p)[1].lower()
if os.path.isdir(fp):
self.scan(fp,bfp)
elif p[0] != "." and e in allowedext:
s = os.stat(fp)
size = s.st_size
width,height,tags = get_image_size(fp)
t = s.st_mtime
imdt = tags.get("Image DateTime")
if imdt is not None:
#2014:09:16 14:27:14
imdt = time.mktime(time.strptime(str(imdt),"%Y:%m:%d %H:%M:%S"))
if imdt is not None and math.fabs(imdt-s.st_mtime) > 1:
os.utime(fp, (imdt,imdt))
print "*",math.fabs(imdt-s.st_mtime)
if height == 1:
width = int(str(tags.get("EXIF ExifImageWidth",str(width))))
height = int(str(tags.get("EXIF ExifImageLength",str(height))))
print fp,t,width,height,imdt
else:
print "skipped",fp
if len(sys.argv) < 2:
print """options:
scancam path
scandisk path
compare
"""
sys.exit(0)
path = sys.argv[1]
s = Scanner()
s.scan(path,"")