-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExifHandler.py
142 lines (123 loc) · 4.26 KB
/
ExifHandler.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
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
################################################################################
# Copyright 2010 Andreas Neustifter (andreas.neustifter@gmail.com)
#
# This file is part of PhotoImport.
#
# PhotoImport 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 3 of the License, or (at your option) any later
# version.
#
# PhotoImport 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
# PhotoImport. If not, see <http://www.gnu.org/licenses/>.
################################################################################
"""
This modules handles exif data for files.
"""
import os
import datetime
try:
import pyexiv2
hasPyExif2=True
except:
hasPyExif2=False
if not hasPyExif2:
try:
import EXIF
hasEXIF=True
except:
hasEXIF=False
import sys
import stat
import subprocess
import wx
import io
import logging
hasExifTool=False
exiftool_name = 'exiftool.exe'
if hasattr(sys, 'frozen'):
application_path = os.path.dirname(sys.executable)
elif __file__:
application_path = os.path.dirname(__file__)
exiftool_path = os.path.join(application_path, exiftool_name)
try:
statinfo = os.stat(exiftool_path)
if (statinfo[stat.ST_MODE] & stat.S_IXUSR) > 0:
hasExifTool=True
except:
pass
def getfiledate(filename):
filedate = None
if hasPyExif2:
exif = pyexiv2.ImageMetadata(filename)
exif.read()
try:
filedate = exif['Exif.Photo.DateTimeOriginal'].value
except:
filedate = exif['Exif.Photo.DateTime'].value
elif hasEXIF:
f = open(filename,'rb')
exif = EXIF.process_file(f, details=False)
try:
filedatestr = exif['EXIF DateTimeOriginal']
except:
filedatestr = exif['EXIF DateTimeDigitized']
filedate = datetime.datetime.strptime(str(filedatestr),"%Y:%m:%d %H:%M:%S")
elif hasExifTool:
filedatestr = subprocess.Popen([exiftool_path,"-b","-DateTimeOriginal",filename],stdout=subprocess.PIPE).communicate()[0]
filedate = datetime.datetime.strptime(filedatestr,"%Y:%m:%d %H:%M:%S")
return filedate
def getthumbnail(imagename):
imagedata = None
if hasPyExif2:
try:
exif = pyexiv2.ImageMetadata(imagename)
exif.read()
except:
logging.error("Can not read EXIF info from file %s, no preview." % imagename)
return
imagetype = ""
if exif.mime_type == 'image/jpeg' or exif.mime_type == 'image/png':
try:
filereader = open(imagename,"rb")
data = filereader.read()
filereader.close()
except:
logging.error("Can not read file %s, no preview." % imagename)
return
imagetype = exif.mime_type
else:
try:
data = exif.previews[-1].data
except:
logging.error("Can not read EXIF preview from file %s, no preview." % imagename)
return
imagetype = 'image/jpeg'
elif hasEXIF:
try:
f = open(imagename,'rb')
exif = EXIF.process_file(f, details=False)
data = exif['JPEGThumbnail']
imagetype = 'image/jpeg'
except:
logging.error("Can not read file %s, no preview." % imagename)
return
elif hasExifTool:
try:
data = subprocess.Popen([exiftool_path,"-b","-ThumbnailImage",imagename],stdout=subprocess.PIPE).communicate()[0]
imagetype = 'image/jpeg'
except:
pass
try:
if imagetype == 'image/jpeg':
imagedata = wx.ImageFromStream(io.BytesIO(data), wx.BITMAP_TYPE_JPEG)
elif imagetype == 'image/png':
imagedata = wx.ImageFromStream(io.BytesIO(data), wx.BITMAP_TYPE_PNG)
except:
logging.error("Can not convert image %s with type %s, no preview." % (imagename, imagetype))
return imagedata