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
|
# -*- coding: utf-8 -*-
# ******************************************************************************
#
# Copyright (C) 2009 Olivier Tilloy <olivier@tilloy.net>
#
# This file is part of the pyexiv2 distribution.
#
# pyexiv2 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.
#
# pyexiv2 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 pyexiv2; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301 USA.
#
# Author: Olivier Tilloy <olivier@tilloy.net>
#
# ******************************************************************************
import unittest
from pyexiv2 import ImageMetadata, ExifTag, IptcTag, XmpTag
import datetime
class ImageMock(object):
def __init__(self, filename):
self.filename = filename
self.read = False
self.written = False
self.tags = {'exif': {}, 'iptc': {}, 'xmp': {}}
def readMetadata(self):
self.read = True
def writeMetadata(self):
self.written = True
def exifKeys(self):
return self.tags['exif'].keys()
def getExifTag(self, key):
return self.tags['exif'][key]
def setExifTagValue(self, key, value):
self.tags['exif'][key] = value
def deleteExifTag(self, key):
try:
del self.tags['exif'][key]
except KeyError:
pass
def iptcKeys(self):
return self.tags['iptc'].keys()
def getIptcTag(self, key):
return self.tags['iptc'][key]
def setIptcTagValues(self, key, values):
self.tags['iptc'][key] = values
def xmpKeys(self):
return self.tags['xmp'].keys()
def getXmpTag(self, key):
return self.tags['xmp'][key]
class TestImageMetadata(unittest.TestCase):
def setUp(self):
self.metadata = ImageMetadata('nofile')
self.metadata._instantiate_image = lambda filename: ImageMock(filename)
def _set_exif_tags(self):
tags = {}
tags['Exif.Image.Make'] = \
('Exif.Image.Make', 'Make', 'Manufacturer', 'blabla', 'Ascii',
'EASTMAN KODAK COMPANY', 'EASTMAN KODAK COMPANY')
tags['Exif.Image.DateTime'] = \
('Exif.Image.DateTime', 'DateTime', 'Date and Time', 'blabla',
'Ascii', '2009:02:09 13:33:20', '2009:02:09 13:33:20')
tags['Exif.Photo.ExifVersion'] = \
('Exif.Photo.ExifVersion', 'ExifVersion', 'Exif Version', 'blabla',
'Undefined', '48 50 50 49 ', '2.21')
self.metadata._image.tags['exif'] = tags
def _set_iptc_tags(self):
tags = {}
tags['Iptc.Application2.Caption'] = \
('Iptc.Application2.Caption', 'Caption', 'Caption',
'A textual description of the object data.', 'String',
['blabla'])
tags['Iptc.Application2.DateCreated'] = \
('Iptc.Application2.DateCreated', 'DateCreated', 'Date Created',
'Represented in the form CCYYMMDD to designate the date the ' \
'intellectual content of the object data was created rather ' \
'than the date of the creation of the physical representation. ' \
'Follows ISO 8601 standard.', 'Date', ['2004-07-13'])
self.metadata._image.tags['iptc'] = tags
def _set_xmp_tags(self):
tags = {}
tags['Xmp.dc.format'] = \
('Xmp.dc.format', 'format', 'Format', 'The file format used when ' \
'saving the resource. Tools and applications should set this ' \
'property to the save format of the data. It may include ' \
'appropriate qualifiers.', 'MIMEType', ['image/jpeg'])
tags['Xmp.dc.subject'] = \
('Xmp.dc.subject', 'subject', 'Subject', 'An unordered array of ' \
'descriptive phrases or keywords that specify the topic of the ' \
'content of the resource.', 'bag Text', ['image, test, pyexiv2'])
tags['Xmp.xmp.CreateDate'] = \
('Xmp.xmp.CreateDate', 'CreateDate', 'Create Date',
'The date and time the resource was originally created.', 'Date',
['2005-09-07T15:07:40-07:00'])
tags['Xmp.xmpMM.DocumentID'] = \
('Xmp.xmpMM.DocumentID', 'DocumentID', 'Document ID',
'The common identifier for all versions and renditions of a ' \
'document. It should be based on a UUID; see Document and ' \
'Instance IDs below.', 'URI',
['uuid:9A3B7F52214211DAB6308A7391270C13'])
self.metadata._image.tags['xmp'] = tags
######################
# Test general methods
######################
def test_read(self):
self.assertEqual(self.metadata._image, None)
self.metadata.read()
self.failIfEqual(self.metadata._image, None)
self.failUnless(self.metadata._image.read)
def test_write(self):
self.metadata.read()
self.failIf(self.metadata._image.written)
self.metadata.write()
self.failUnless(self.metadata._image.written)
###########################
# Test EXIF-related methods
###########################
def test_exif_keys(self):
self.metadata.read()
self._set_exif_tags()
self.assertEqual(self.metadata._keys['exif'], None)
keys = self.metadata.exif_keys
self.assertEqual(len(keys), 3)
self.assertEqual(self.metadata._keys['exif'], keys)
def test_get_exif_tag(self):
self.metadata.read()
self._set_exif_tags()
self.assertEqual(self.metadata._tags['exif'], {})
# Get an existing tag
key = 'Exif.Image.Make'
tag = self.metadata._get_exif_tag(key)
self.assertEqual(type(tag), ExifTag)
self.assertEqual(tag.metadata, self.metadata)
self.assertEqual(self.metadata._tags['exif'][key], tag)
# Try to get an nonexistent tag
key = 'Exif.Photo.Sharpness'
self.failUnlessRaises(KeyError, self.metadata._get_exif_tag, key)
def test_set_exif_tag_wrong(self):
self.metadata.read()
self._set_exif_tags()
self.assertEqual(self.metadata._tags['exif'], {})
# Try to set a tag with wrong type
tag = 'Not an exif tag'
self.failUnlessRaises(TypeError, self.metadata._set_exif_tag, tag)
self.assertEqual(self.metadata._tags['exif'], {})
def test_set_exif_tag_create(self):
self.metadata.read()
self._set_exif_tags()
self.assertEqual(self.metadata._tags['exif'], {})
# Create a new tag
tag = ExifTag('Exif.Thumbnail.Orientation', 'Orientation',
'Orientation', 'The image orientation viewed in terms ' \
'of rows and columns.', 'Short', '1', 'top, left')
self.assertEqual(tag.metadata, None)
self.metadata._set_exif_tag(tag)
self.assertEqual(tag.metadata, self.metadata)
self.assertEqual(self.metadata._tags['exif'], {tag.key: tag})
self.assert_(self.metadata._image.tags['exif'].has_key(tag.key))
self.assertEqual(self.metadata._image.tags['exif'][tag.key],
tag.raw_value)
def test_set_exif_tag_overwrite(self):
self.metadata.read()
self._set_exif_tags()
self.assertEqual(self.metadata._tags['exif'], {})
# Overwrite an existing tag
tag = ExifTag('Exif.Image.DateTime', 'DateTime', 'Date and Time',
'blabla', 'Ascii', '2009:03:20 20:32:00',
'2009:03:20 20:32:00')
self.assertEqual(tag.metadata, None)
self.metadata._set_exif_tag(tag)
self.assertEqual(tag.metadata, self.metadata)
self.assertEqual(self.metadata._tags['exif'], {tag.key: tag})
self.assert_(self.metadata._image.tags['exif'].has_key(tag.key))
self.assertEqual(self.metadata._image.tags['exif'][tag.key],
tag.raw_value)
def test_set_exif_tag_overwrite_already_cached(self):
self.metadata.read()
self._set_exif_tags()
self.assertEqual(self.metadata._tags['exif'], {})
# Overwrite an existing tag already cached
key = 'Exif.Photo.ExifVersion'
tag = self.metadata._get_exif_tag(key)
self.assertEqual(self.metadata._tags['exif'][key], tag)
new_tag = ExifTag(key, 'ExifVersion', 'Exif Version', 'blabla',
'Undefined', '48 50 50 48 ', '2.20')
self.assertEqual(new_tag.metadata, None)
self.metadata._set_exif_tag(new_tag)
self.assertEqual(new_tag.metadata, self.metadata)
self.assertEqual(self.metadata._tags['exif'], {key: new_tag})
self.assert_(self.metadata._image.tags['exif'].has_key(key))
# Special case where the formatted value is used instead of the raw
# value.
self.assertEqual(self.metadata._image.tags['exif'][key], new_tag.fvalue)
def test_set_exif_tag_value_inexistent(self):
self.metadata.read()
self._set_exif_tags()
key = 'Exif.Photo.ExposureTime'
value = '1/500'
self.failUnlessRaises(KeyError, self.metadata._set_exif_tag_value,
key, value)
def test_set_exif_tag_value_wrong_type(self):
self.metadata.read()
self._set_exif_tags()
key = 'Exif.Image.DateTime'
value = datetime.datetime(2009, 3, 24, 9, 37, 36)
self.failUnlessRaises(TypeError, self.metadata._set_exif_tag_value,
key, value)
def test_set_exif_tag_value(self):
self.metadata.read()
self._set_exif_tags()
key = 'Exif.Image.DateTime'
tag = self.metadata._get_exif_tag(key)
value = '2009:03:24 09:37:36'
self.failIfEqual(self.metadata._image.tags['exif'][key], value)
self.metadata._set_exif_tag_value(key, value)
self.assertEqual(self.metadata._image.tags['exif'][key], value)
def test_delete_exif_tag_inexistent(self):
self.metadata.read()
self._set_exif_tags()
key = 'Exif.Image.Artist'
self.failUnlessRaises(KeyError, self.metadata._delete_exif_tag, key)
def test_delete_exif_tag_not_cached(self):
self.metadata.read()
self._set_exif_tags()
key = 'Exif.Image.DateTime'
self.assertEqual(self.metadata._tags['exif'], {})
self.assert_(self.metadata._image.tags['exif'].has_key(key))
self.metadata._delete_exif_tag(key)
self.assertEqual(self.metadata._tags['exif'], {})
self.failIf(self.metadata._image.tags['exif'].has_key(key))
def test_delete_exif_tag_cached(self):
self.metadata.read()
self._set_exif_tags()
key = 'Exif.Image.DateTime'
self.assert_(self.metadata._image.tags['exif'].has_key(key))
tag = self.metadata._get_exif_tag(key)
self.assertEqual(self.metadata._tags['exif'][key], tag)
self.metadata._delete_exif_tag(key)
self.assertEqual(self.metadata._tags['exif'], {})
self.failIf(self.metadata._image.tags['exif'].has_key(key))
###########################
# Test IPTC-related methods
###########################
def test_iptc_keys(self):
self.metadata.read()
self._set_iptc_tags()
self.assertEqual(self.metadata._keys['iptc'], None)
keys = self.metadata.iptc_keys
self.assertEqual(len(keys), 2)
self.assertEqual(self.metadata._keys['iptc'], keys)
def test_get_iptc_tag(self):
self.metadata.read()
self._set_iptc_tags()
self.assertEqual(self.metadata._tags['iptc'], {})
# Get an existing tag
key = 'Iptc.Application2.DateCreated'
tag = self.metadata._get_iptc_tag(key)
self.assertEqual(type(tag), IptcTag)
self.assertEqual(tag.metadata, self.metadata)
self.assertEqual(self.metadata._tags['iptc'][key], tag)
# Try to get an nonexistent tag
key = 'Iptc.Application2.Copyright'
self.failUnlessRaises(KeyError, self.metadata._get_iptc_tag, key)
def test_set_iptc_tag_wrong(self):
self.metadata.read()
self._set_iptc_tags()
self.assertEqual(self.metadata._tags['iptc'], {})
# Try to set a tag with wrong type
tag = 'Not an iptc tag'
self.failUnlessRaises(TypeError, self.metadata._set_iptc_tag, tag)
self.assertEqual(self.metadata._tags['iptc'], {})
def test_set_iptc_tag_create(self):
self.metadata.read()
self._set_iptc_tags()
self.assertEqual(self.metadata._tags['iptc'], {})
# Create a new tag
tag = IptcTag('Iptc.Application2.Writer', 'Writer', 'Writer',
'Identification of the name of the person involved in ' \
'the writing, editing or correcting the object data or ' \
'caption/abstract.', 'String', ['Nobody'])
self.assertEqual(tag.metadata, None)
self.metadata._set_iptc_tag(tag)
self.assertEqual(tag.metadata, self.metadata)
self.assertEqual(self.metadata._tags['iptc'], {tag.key: tag})
self.assert_(self.metadata._image.tags['iptc'].has_key(tag.key))
self.assertEqual(self.metadata._image.tags['iptc'][tag.key],
tag.raw_value)
def test_set_iptc_tag_overwrite(self):
self.metadata.read()
self._set_iptc_tags()
self.assertEqual(self.metadata._tags['iptc'], {})
# Overwrite an existing tag
tag = IptcTag('Iptc.Application2.Caption', 'Caption', 'Caption',
'A textual description of the object data.', 'String',
['A picture.'])
self.assertEqual(tag.metadata, None)
self.metadata._set_iptc_tag(tag)
self.assertEqual(tag.metadata, self.metadata)
self.assertEqual(self.metadata._tags['iptc'], {tag.key: tag})
self.assert_(self.metadata._image.tags['iptc'].has_key(tag.key))
self.assertEqual(self.metadata._image.tags['iptc'][tag.key],
tag.raw_value)
def test_set_iptc_tag_overwrite_already_cached(self):
self.metadata.read()
self._set_iptc_tags()
self.assertEqual(self.metadata._tags['iptc'], {})
# Overwrite an existing tag already cached
key = 'Iptc.Application2.Caption'
tag = self.metadata._get_iptc_tag(key)
self.assertEqual(self.metadata._tags['iptc'][key], tag)
new_tag = IptcTag(key, 'Caption', 'Caption',
'A textual description of the object data.', 'String',
['A picture.'])
self.assertEqual(new_tag.metadata, None)
self.metadata._set_iptc_tag(new_tag)
self.assertEqual(new_tag.metadata, self.metadata)
self.assertEqual(self.metadata._tags['iptc'], {key: new_tag})
self.assert_(self.metadata._image.tags['iptc'].has_key(key))
self.assertEqual(self.metadata._image.tags['iptc'][key],
new_tag.raw_value)
def test_set_iptc_tag_values_inexistent(self):
self.metadata.read()
self._set_iptc_tags()
key = 'Iptc.Application2.Urgency'
values = ['1']
self.failUnlessRaises(KeyError, self.metadata._set_iptc_tag_values,
key, values)
def test_set_iptc_tag_values_wrong_type(self):
self.metadata.read()
self._set_iptc_tags()
key = 'Iptc.Application2.DateCreated'
value = '20090324'
self.failUnlessRaises(TypeError, self.metadata._set_iptc_tag_values,
key, value)
values = [datetime.date(2009, 3, 24)]
self.failUnlessRaises(TypeError, self.metadata._set_iptc_tag_values,
key, values)
def test_set_iptc_tag_values(self):
self.metadata.read()
self._set_iptc_tags()
key = 'Iptc.Application2.DateCreated'
tag = self.metadata._get_iptc_tag(key)
values = ['2009-04-07']
self.failIfEqual(self.metadata._image.tags['iptc'][key], values)
self.metadata._set_iptc_tag_values(key, values)
self.assertEqual(self.metadata._image.tags['iptc'][key], values)
def test_delete_iptc_tag_inexistent(self):
# TODO
raise(NotImplementedError())
def test_delete_iptc_tag_not_cached(self):
# TODO
raise(NotImplementedError())
def test_delete_iptc_tag_cached(self):
# TODO
raise(NotImplementedError())
##########################
# Test XMP-related methods
##########################
def test_xmp_keys(self):
self.metadata.read()
self._set_xmp_tags()
self.assertEqual(self.metadata._keys['xmp'], None)
keys = self.metadata.xmp_keys
self.assertEqual(len(keys), 4)
self.assertEqual(self.metadata._keys['xmp'], keys)
def test_get_xmp_tag(self):
self.metadata.read()
self._set_xmp_tags()
self.assertEqual(self.metadata._tags['xmp'], {})
# Get an existing tag
key = 'Xmp.dc.subject'
tag = self.metadata._get_xmp_tag(key)
self.assertEqual(type(tag), XmpTag)
self.assertEqual(tag.metadata, self.metadata)
self.assertEqual(self.metadata._tags['xmp'][key], tag)
# Try to get an nonexistent tag
key = 'Xmp.xmp.Label'
self.failUnlessRaises(KeyError, self.metadata._get_xmp_tag, key)
def test_set_xmp_tag_wrong(self):
# TODO
raise(NotImplementedError())
def test_set_xmp_tag_create(self):
# TODO
raise(NotImplementedError())
def test_set_xmp_tag_overwrite(self):
# TODO
raise(NotImplementedError())
def test_set_xmp_tag_overwrite_already_cached(self):
# TODO
raise(NotImplementedError())
def test_set_xmp_tag_value_inexistent(self):
# TODO
raise(NotImplementedError())
def test_set_xmp_tag_value_wrong_type(self):
# TODO
raise(NotImplementedError())
def test_set_xmp_tag_value(self):
# TODO
raise(NotImplementedError())
def test_delete_xmp_tag_inexistent(self):
# TODO
raise(NotImplementedError())
def test_delete_xmp_tag_not_cached(self):
# TODO
raise(NotImplementedError())
def test_delete_xmp_tag_cached(self):
# TODO
raise(NotImplementedError())
###########################
# Test dictionary interface
###########################
def test_getitem(self):
self.metadata.read()
self._set_exif_tags()
self._set_iptc_tags()
self._set_xmp_tags()
# Get existing tags
key = 'Exif.Photo.ExifVersion'
tag = self.metadata[key]
self.assertEqual(type(tag), ExifTag)
key = 'Iptc.Application2.Caption'
tag = self.metadata[key]
self.assertEqual(type(tag), IptcTag)
key = 'Xmp.xmp.CreateDate'
tag = self.metadata[key]
self.assertEqual(type(tag), XmpTag)
# Try to get nonexistent tags
keys = ('Exif.Image.SamplesPerPixel', 'Iptc.Application2.FixtureId',
'Xmp.xmp.Rating', 'Wrong.Noluck.Raise')
for key in keys:
self.failUnlessRaises(KeyError, self.metadata.__getitem__, key)
def test_setitem(self):
# TODO
raise(NotImplementedError())
def test_delitem(self):
# TODO
raise(NotImplementedError())
|