-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpy3exif.py
308 lines (231 loc) · 8.98 KB
/
py3exif.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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Copyright (C) 2017 Sean Davis
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 3 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, see <http://www.gnu.org/licenses/>.
"""
__author__ = "Sean Davis"
__copyright__ = "Copyright (C) 2017 Sean Davis"
__license__ = "GNU General Public License"
__version__ = "0.1"
import os
import re
import sys
import pathlib
import platform
import subprocess
_create_formats = ["DR4", "EXIF", "EXV", "ICC", "ICM", "MIE", "ODI", "ODP",
"ODS", "ODT", "VRD", "XMP"]
_read_formats = ["3FR", "3G2", "3GP2", "3GP", "3GPP", "A", "AA", "AAX", "ACR",
"AFM", "ACFM", "AMFM", "AI", "AIT", "AIFF", "AIF", "AIFC", "APE", "ARW", "ASF",
"AVI", "BMP", "DIB", "BPG", "BTF", "CHM", "COS", "CR2", "CRW", "CIFF", "CS1",
"DCM", "DC3", "DIC", "DICM", "DCP", "DCR", "DFONT", "DIVX", "DJVU", "DJV",
"DNG", "DOC", "DOT", "DOCX", "DOCM", "DOTX", "DOTM", "DPX", "DR4", "DSS", "DS2",
"DYLIB", "DV", "DVB", "EIP", "EPS", "EPSF", "PS", "EPUB", "ERF", "EXE", "DLL",
"EXIF", "EXR", "EXV", "F4A", "F4B", "F4P", "F4V", "FFF", "FFF", "FLA", "FLAC",
"FLIF", "FLV", "FPF", "FPX", "GIF", "GZ", "GZIP", "HDP", "WDP", "JXR", "HDR",
"HEIC", "HEIF", "HTML", "HTM", "XHTML", "ICC", "ICM", "ICS", "ICAL", "IDML",
"IIQ", "IND", "INDD", "INDT", "INX", "ISO", "ITC", "J2C", "JPC", "JP2", "JPF",
"J2K", "JPM", "JPX", "JPEG", "JPG", "JPE", "JSON", "K25", "KDC", "KEY", "KTH",
"LA", "LFP", "LFR", "LNK", "M2TS", "MTS", "M2T", "TS", "M4A", "M4B", "M4P",
"M4V", "MAX", "MEF", "MIE", "MIFF", "MIF", "MKA", "MKV", "MKS", "MOBI", "AZW",
"AZW3", "MODD", "MOI", "MOS", "MOV", "QT", "MP3", "MP4", "MPC", "MPEG", "MPG",
"M2V", "MPO", "MQV", "MRW", "MXF", "NEF", "NMBTEMPLATE", "NRW", "NUMBERS", "O",
"ODB", "ODC", "ODF", "ODG,", "ODI", "ODP", "ODS", "ODT", "OFR", "OGG", "OGV",
"OPUS", "ORF", "OTF", "PAC", "PAGES", "PCD", "PDB", "PRC", "PDF", "PEF", "PFA",
"PFB", "PFM", "PGF", "PICT", "PCT", "PLIST", "PMP", "PNG, JNG", "MNG", "PPM",
"PBM", "PGM", "PPT", "PPS", "POT", "POTX", "POTM", "PPSX", "PPSM", "PPTX",
"PPTM", "PSD", "PSB", "PSDT", "PSP", "PSPIMAGE", "QTIF", "QTI", "QIF", "RA",
"RAF", "RAM", "RPM", "RAR", "RAW", "RAW", "RIFF", "RIF", "RM", "RV", "RMVB",
"RSRC", "RTF", "RW2", "RWL", "RWZ", "SEQ", "SO", "SR2", "SRF", "SRW", "SVG",
"SWF", "THM", "THMX", "TIFF", "TIF", "TTF", "TTC", "TORRENT", "VCF", "VCARD",
"VOB", "VRD", "VSD", "WAV", "WEBM", "WEBP", "WMA", "WMV", "WV", "X3F", "XCF",
"XLS", "XLT", "XLSX", "XLSM", "XLSB", "XLTX", "XLTM", "XMP", "ZIP"]
_write_formats = ["3G2", "3GP2", "3GP", "3GPP", "AAX", "AI", "AIT", "ARW",
"CR2", "CRW", "CIFF", "CS1", "DCP", "DNG", "DVB", "EPS", "EPSF", "PS", "ERF",
"F4A", "F4B", "F4P", "F4V", "FFF", "FLIF", "GIF", "HDP", "WDP", "JXR", "IIQ",
"IND", "INDD", "INDT", "JP2", "JPF", "J2K", "JPM", "JPX", "JPEG", "JPG", "JPE",
"M4A", "M4B", "M4P", "M4V", "MEF", "MOS", "MOV", "QT", "MP4", "MPO", "MQV",
"MRW", "NEF", "NRW", "ODI", "ODP", "ODS", "ODT", "ORF", "PDF", "PEF", "PNG",
"JNG", "MNG", "PPM", "PBM", "PGM", "PSD", "PSB", "PSDT", "QTIF", "QTI", "QIF",
"RAF", "RAW", "RW2", "RWL", "SR2", "SRW", "THM", "TIFF", "TIF", "X3F"]
def _cleanup_data( data, datatype ):
if datatype == "string":
x = str( data )
elif datatype == "list":
x = list( filter(
None, [ re.sub( r"\s+", " ", s ) for s in data.split("\n") ] ) )
elif datatype == "dict":
y = list( filter(
None, [ re.sub( r"\s+", " ", s ) for s in data.split("\n") ] ) )
x = { k:v for k,v in ( i.split(' : ') for i in y ) }
return x
def _generate_cmd( ):
"""not in use"""
pass
def _run_command( cmd ):
proc = subprocess.Popen( cmd, stdout=subprocess.PIPE, shell=True )
output, err = proc.communicate()
proc.wait()
return str(output, 'UTF-8', 'ignore')
def _verify_file( c, f ):
if not pathlib.Path( f ).is_file():
return ( "{} is not a valid file." ).format( f )
if c == "create":
formats = _create_formats
elif c == "read":
formats = _read_formats
else:
formats = _write_formats
if not any( pathlib.Path( f ).suffix[1:].upper() in x for x in formats ):
return ("{} is not a valid file type to {}.").format( f, c )
try:
fp = open( f )
except IOError as e:
return ("You do not have permissions to {} the file {}.").format( c, f )
else:
fp.close()
return True
def _verify_installation( x ):
pctype = platform.system()
if x is None:
if ( pctype.lower() == "darwin" or "linux" in pctype.lower() ):
cmd = "exiftool"
else:
cmd = "exiftool.exe"
else:
if ( pctype.lower() == "darwin" or "linux" in pctype.lower() ):
if ( pathlib.Path( x ).is_file() or x == "exiftool" ):
cmd = x
else:
cmd = "exiftool"
else:
if ( pathlib.Path( x ).is_file() or "exiftool.exe" in x ):
cmd = x
else:
cmd = "exiftool.exe"
try:
results = _run_command( cmd )
except Exception as e:
print ( e )
raise RuntimeError( "Running this requires exiftool installed." )
else:
if any( a in str( results ) for a in ["command not found", "is not recognized"] ):
raise RuntimeError( "Running this requires exiftool installed." )
return cmd
class py3exifcopy( object ):
def __init__( self, path=None, overwrite=False ):
self.exiftool = _verify_installation( path )
self.overwrite = overwrite
def _check_sidecar( self, sidecar ):
if sidecar is None:
result = False
else:
if pathlib.Path( sidecar ).is_file():
result = sidecar
else:
raise RuntimeError( "Not a valid sidecar file." )
return result
def all( self, args, sidecar=None ):
"""exiftool -tagsfromfile file.ext -all:all output.ext"""
pass
def all_iptc( self, args, sidecar=None ):
"""exiftool -tagsfromfile file.ext -iptc:all output.ext"""
pass
def all_xmp( self, args, sidecar=None ):
"""exiftool -tagsfromfile file.ext -xmp:all output.ext"""
pass
def custom( self, args ):
"""custom arguments"""
pass
class py3exifread( object ):
def __init__( self, path=None ):
self.exiftool = _verify_installation( path )
def all_contains( self, terms, filepath, datatype="string" ):
valid = _verify_file( "read", filepath )
if valid == True:
cmd = '"{}" "-*{}*" "{}"'.format( self.exiftool, terms, filepath )
results = _run_command( cmd )
data = _cleanup_data( results, datatype )
else:
return valid
return data
def all_data( self, filepath, datatype="string" ):
valid = _verify_file( "read", filepath )
if valid == True:
cmd = '"{}" "{}"'.format( self.exiftool, filepath )
results = _run_command( cmd )
data = _cleanup_data( results, datatype )
else:
return valid
return data
def all_duplicates( self, filepath, datatype="string" ):
valid = _verify_file( "read", filepath )
if valid == True:
cmd = '"{}" -a -u "{}"'.format( self.exiftool, filepath )
results = _run_command( cmd )
data = _cleanup_data( results, datatype )
else:
return valid
return data
def common( self, filepath, datatype="string" ):
valid = _verify_file( "read", filepath )
if valid == True:
cmd = '"{}" -common "{}"'.format( self.exiftool, filepath )
results = _run_command( cmd )
data = _cleanup_data( results, datatype )
else:
return valid
return data
def keywords( self, filepath, datatype="string" ):
valid = _verify_file( "read", filepath )
if valid == True:
cmd = '"{}" -keywords "{}"'.format( self.exiftool, filepath )
results = _run_command( cmd )
data = _cleanup_data( results, datatype )
else:
return valid
return data
def custom( self, args, filepath, datatype="string" ):
valid = _verify_file( "read", filepath )
if args == None:
return "Invalid Arguments"
if valid == True:
cmd = '"{}" {} "{}"'.format( self.exiftool, args, filepath )
results = _run_command( cmd )
data = _cleanup_data( results, datatype )
else:
return valid
return data
class py3exifwrite( object ):
def __init__( self, path=None, overwrite=False ):
self.exiftool = _verify_installation( path )
self.exiftool = path
self.overwrite = overwrite
def keywords( self, data="append" ):
"""exiftool -iptc:keywords+="""
pass
def custom( self ):
"""custom arguments"""
pass
def usage():
print ("""
To use this module, create an instance of either the copy, read, or
write class, passing in the exiftool path (if applicable), and the path
to the image to be handled. You may also pass in whether you want the
program to automatically keep a backup of your original photo
(default=False). If a backup is created, it will be in the same location
as the original, with "_ORIGINAL" appended to the file name. Once you
have an editor instance, you call its methods to get information about
the image, or to modify the image's metadata.""")
if __name__ == "__main__":
usage()