257
by Olivier Tilloy
Removed some unused legacy code. |
1 |
#!/usr/bin/python
|
235.1.11
by Olivier Tilloy
Added an example script that manipulates EXIF metadata in an image. |
2 |
# -*- coding: utf-8 -*-
|
3 |
||
275.1.3
by Olivier Tilloy
Adapted the examples to make use of direct value assignment, |
4 |
from pyexiv2 import ImageMetadata |
235.1.45
by Olivier Tilloy
Updated the example script to also manipulate IPTC and XMP metadata. |
5 |
|
6 |
import sys, os |
|
7 |
from datetime import datetime, date |
|
8 |
||
9 |
||
10 |
def print_key_value(metadata, key): |
|
11 |
print key, '=', metadata[key] |
|
235.1.11
by Olivier Tilloy
Added an example script that manipulates EXIF metadata in an image. |
12 |
|
13 |
||
14 |
if __name__ == '__main__': |
|
15 |
# Read an image file's metadata
|
|
16 |
image_file = sys.argv[1] |
|
17 |
metadata = ImageMetadata(image_file) |
|
18 |
metadata.read() |
|
19 |
||
20 |
# Print a list of all the keys of the EXIF tags in the image
|
|
235.1.45
by Olivier Tilloy
Updated the example script to also manipulate IPTC and XMP metadata. |
21 |
print 'EXIF keys:', metadata.exif_keys |
22 |
||
23 |
try: |
|
24 |
# Print the value of the Exif.Image.DateTime tag
|
|
25 |
key = 'Exif.Image.DateTime' |
|
26 |
print_key_value(metadata, key) |
|
27 |
||
28 |
# Set the value of the Exif.Image.DateTime tag
|
|
275.1.3
by Olivier Tilloy
Adapted the examples to make use of direct value assignment, |
29 |
metadata[key] = datetime.now() |
235.1.45
by Olivier Tilloy
Updated the example script to also manipulate IPTC and XMP metadata. |
30 |
print_key_value(metadata, key) |
31 |
except KeyError: |
|
32 |
print '[not set]' |
|
33 |
||
34 |
# Add a new tag
|
|
35 |
key = 'Exif.Image.Orientation' |
|
275.1.3
by Olivier Tilloy
Adapted the examples to make use of direct value assignment, |
36 |
metadata[key] = 1 |
235.1.45
by Olivier Tilloy
Updated the example script to also manipulate IPTC and XMP metadata. |
37 |
print_key_value(metadata, key) |
38 |
||
39 |
# Print a list of all the keys of the IPTC tags in the image
|
|
40 |
print os.linesep, 'IPTC keys:', metadata.iptc_keys |
|
41 |
||
42 |
try: |
|
43 |
# Print the value of the Iptc.Application2.DateCreated tag
|
|
44 |
key = 'Iptc.Application2.DateCreated' |
|
45 |
print_key_value(metadata, key) |
|
46 |
||
47 |
# Set the value of the Iptc.Application2.DateCreated tag
|
|
275.1.3
by Olivier Tilloy
Adapted the examples to make use of direct value assignment, |
48 |
metadata[key] = [date.today()] |
235.1.45
by Olivier Tilloy
Updated the example script to also manipulate IPTC and XMP metadata. |
49 |
print_key_value(metadata, key) |
50 |
except KeyError: |
|
51 |
print '[not set]' |
|
52 |
||
53 |
# Add a new tag
|
|
54 |
key = 'Iptc.Application2.Keywords' |
|
55 |
keywords = ['little', 'big', 'man'] |
|
275.1.3
by Olivier Tilloy
Adapted the examples to make use of direct value assignment, |
56 |
metadata[key] = keywords |
235.1.45
by Olivier Tilloy
Updated the example script to also manipulate IPTC and XMP metadata. |
57 |
print_key_value(metadata, key) |
58 |
||
59 |
# Print a list of all the keys of the XMP tags in the image
|
|
60 |
print os.linesep, 'XMP keys:', metadata.xmp_keys |
|
61 |
||
62 |
try: |
|
63 |
# Print the value of the Xmp.dc.subject tag
|
|
64 |
key = 'Xmp.dc.subject' |
|
65 |
print_key_value(metadata, key) |
|
66 |
||
67 |
# Set the value of the Xmp.dc.subject tag
|
|
275.1.3
by Olivier Tilloy
Adapted the examples to make use of direct value assignment, |
68 |
metadata[key] = keywords |
235.1.45
by Olivier Tilloy
Updated the example script to also manipulate IPTC and XMP metadata. |
69 |
print_key_value(metadata, key) |
70 |
except KeyError: |
|
71 |
print '[not set]' |
|
72 |
||
73 |
# Add a new tag
|
|
74 |
key = 'Xmp.dc.title' |
|
275.1.3
by Olivier Tilloy
Adapted the examples to make use of direct value assignment, |
75 |
metadata[key] = {'x-default': 'Sunset', 'fr': 'Coucher de soleil'} |
235.1.45
by Olivier Tilloy
Updated the example script to also manipulate IPTC and XMP metadata. |
76 |
print_key_value(metadata, key) |
235.1.11
by Olivier Tilloy
Added an example script that manipulates EXIF metadata in an image. |
77 |
|
78 |
# Write back the metadata to the file
|
|
79 |
metadata.write() |
|
80 |