-
Notifications
You must be signed in to change notification settings - Fork 2
/
displayExiv2.py
303 lines (266 loc) · 11.1 KB
/
displayExiv2.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
# -*- coding: utf-8 -*-
#!/usr/bin/env python
# displayExiv2 module
#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2009-2011 Rob G. Healey <[email protected]>
# 2019 Paul Culley <[email protected]>
# 2022 Arnold Wiegert [email protected]>
#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#-------------------------------------------------------------------------
#
# GNOME modules
#
#-------------------------------------------------------------------------
from gi.repository import GObject, Gtk
## display image metadata using GExiv2 library instead of Exiftool
# *****************************************************************************
# Python Modules
# *****************************************************************************
import os
"""
Display Exiv2 Gramplet
"""
#-------------------------------------------------------------------------
#
# Gramps modules
#
#-------------------------------------------------------------------------
from gramps.gen.plug import Gramplet
from gramps.gen.utils.file import media_path_full
from gi.repository import Gtk
import gi
gi.require_version('GExiv2', '0.10')
from gi.repository import GExiv2
##from gi.repository.GExiv2 import Metadata
# code from: - only good for JPGs
# https://www.thepythoncode.com/article/extracting-image-metadata-in-python
from PIL import Image
from PIL.ExifTags import TAGS
#-------------------------------------------------------------------------
#
# Gramps modules
#
#-------------------------------------------------------------------------
from gramps.gui.listmodel import ListModel
from gramps.gen.const import GRAMPS_LOCALE as glocale
_ = glocale.translation.gettext
from gramps.gen.utils.place import conv_lat_lon
from fractions import Fraction
from gramps.gen.lib import Date
from gramps.gen.datehandler import displayer
from datetime import datetime
# ----------------------------------------------------------
class DisplayExiv2(Gramplet):
"""
Displays the metadata of an image.
"""
def init(self):
self.defer_draw = False
self.defer_draw_id = None
self.set_text( "Exiv2 Metadata" )
gexiv2Version = GExiv2.get_version()
self.gui.WIDGET = self.build_gui()
self.gui.get_container_widget().remove(self.gui.textview)
self.gui.get_container_widget().add(self.gui.WIDGET)
self.gui.WIDGET.show()
# ----------------------------------------------------------
def db_changed(self):
self.connect_signal('Media', self.update)
# ----------------------------------------------------------
def build_gui(self):
"""
Build the GUI interface.
"""
self.view = MetadataView3()
return self.view
# ----------------------------------------------------------
def main(self):
if not self.defer_draw_id:
self.defer_draw_id = GObject.timeout_add(250, self.handle_draw)
else:
self.defer_draw = True
def handle_draw(self):
if self.defer_draw:
self.defer_draw = False
return True
active_handle = self.get_active('Media')
if active_handle:
media = self.dbstate.db.get_media_from_handle(active_handle)
if media:
full_path = media_path_full(self.dbstate.db, media.get_path())
has_data = self.view.display_metadata(full_path)
self.set_has_data(has_data)
else:
self.set_has_data(False)
else:
self.set_has_data(False)
GObject.source_remove(self.defer_draw_id)
self.defer_draw_id = None
return False
# ----------------------------------------------------------
class MetadataView3(Gtk.TreeView):
def __init__(self):
Gtk.TreeView.__init__(self)
self.sections = {}
titles = [(_('Key'), 1, 235),
(_('Value'), 2, 325)]
self.model = ListModel(self, titles, list_mode="tree")
# ----------------------------------------------------------
def display_metadata(self, full_path):
"""
Display the metadata
"""
self.sections = {}
self.model.clear()
ver = GExiv2.get_version()
""" Make sure the file exists"""
if not os.path.exists(full_path):
head, tail = os.path.split( full_path )
label = tail
node = self.__add_section('File not found')
label = tail
human_value = head
self.model.add((label, human_value), node=node)
return False
lastLeadin = ""
retval = False
#logLeadIn = True
logLeadIn = False
#logData = True
logData = False
with open(full_path, 'rb') as fd:
try:
buf = fd.read()
metadata = GExiv2.Metadata()
metadata.open_buf(buf)
# # add header to identify GExiv2
label = 'GExiv2 Version'
node = self.__add_section("Metadata")
self.model.add((label, GExiv2._version), node=node)
self.model.add((label, str(ver) ), node=node)
get_human = metadata.get_tag_interpreted_string
# handle Exif tags
for key in metadata.get_exif_tags():
tagType = metadata.get_tag_type(key)
label = metadata.get_tag_label(key)
human_value = get_human(key)
leadin = key.split('.',2)
section = leadin[0]
if logData:
print(f"{key:50}: {human_value}")
if lastLeadin != section:
lastLeadin = section
if logLeadIn:
print( " section: " + section +" lastLeadin: " + lastLeadin + "\n" )
node = self.__add_section(section)
if human_value is None:
human_value = ''
self.model.add((label, human_value), node=node)
# handle IPTC tags
for key in metadata.get_iptc_tags():
label = metadata.get_tag_label(key)
human_value = get_human(key)
leadin = key.split('.',3)
section = leadin[0]
if logData:
print(f"{key:50}: {human_value}")
if lastLeadin != section:
lastLeadin = section
if logLeadIn:
print( " section: " + section +" lastLeadin: " + lastLeadin + "\n" )
cleanName = leadin[2]
# See the IPTC Spec IIMV4.1.pdf
if cleanName == "CharacterSet":
if human_value == "\x1b%G":
human_value = 'UTF8 - ESC%G'
else:
temp = human_value
human_value = 'Unknown char set: ' + temp
node = self.__add_section(section)
if human_value is None:
human_value = ''
self.model.add((label, human_value), node=node)
# handle XMP tags
for key in metadata.get_xmp_tags():
label = metadata.get_tag_label(key)
human_value = get_human(key)
leadin = key.split('.',3)
section = leadin[0]
if logData:
print(f"{key:50}: {human_value}")
if lastLeadin != section:
lastLeadin = section
if logLeadIn:
print( " section: " + section +" lastLeadin: " + lastLeadin + "\n" )
cleanName = leadin[2]
# # See the IPTC Spec IIMV4.1.pdf
# if cleanName == "CharacterSet":
# if human_value == "\x1b%G":
# human_value = 'UTF8 - \x1b%G'
# else:
# temp = human_value
# human_value = 'Unknown char set: ' + temp
node = self.__add_section(section)
if human_value is None:
human_value = ''
self.model.add((label, human_value), node=node)
#n = self.model.count
if self.model.count <= 3:
head, tail = os.path.split( full_path )
label = tail
node = self.__add_section('No Metadata found in: ')
label = tail
human_value = ''
self.model.add((label, human_value), node=node)
self.model.tree.expand_all()
#retval = self.model.count > 0
except:
pass
return retval
# ----------------------------------------------------------
def __add_section(self, section):
"""
Add the section heading node to the model.
"""
if section not in self.sections:
node = self.model.add([section, ''])
self.sections[section] = node
else:
node = self.sections[section]
return node
# ----------------------------------------------------------
def get_has_data(self, full_path):
"""
Return True if the gramplet has data, else return False.
"""
if not os.path.exists(full_path):
return False
with open(full_path, 'rb') as fd:
retval = False
try:
buf = fd.read()
metadata = GExiv2.Metadata()
metadata.open_buf(buf)
for tag in TAGS:
if tag in metadata.get_exif_tags():
retval = True
break
except:
pass
return retval
# ---------------------------- eof ------------------------------