~osomon/pyexiv2/pyexiv2-0.3

268.1.6 by Olivier Tilloy
Basic structure of the documentation.
1
Tutorial
2
========
3
268.1.16 by Olivier Tilloy
Tutorial: reading and writing EXIF tags.
4
This tutorial is meant to give you a quick overview of what pyexiv2 allows you
5
to do. You can just read it through or follow it interactively, in which case
6
you will need to have pyexiv2 installed.
268.1.17 by Olivier Tilloy
Tutorial: reading and writing IPTC tags.
7
It doesn't cover all the possibilities offered by pyexiv2, only a basic subset
8
of them. For complete reference, see the :doc:`api`.
268.1.16 by Olivier Tilloy
Tutorial: reading and writing EXIF tags.
9
10
Let's get started!
11
12
First of all, we import the pyexiv2 module::
13
14
  >>> import pyexiv2
15
268.1.17 by Olivier Tilloy
Tutorial: reading and writing IPTC tags.
16
We then load an image file and read its metadata::
268.1.16 by Olivier Tilloy
Tutorial: reading and writing EXIF tags.
17
18
  >>> metadata = pyexiv2.ImageMetadata('test.jpg')
19
  >>> metadata.read()
20
21
Reading and writing EXIF tags
22
#############################
23
24
Let's retrieve a list of all the available EXIF tags available in the image::
25
26
  >>> metadata.exif_keys
27
  ['Exif.Image.ImageDescription',
28
   'Exif.Image.XResolution',
29
   'Exif.Image.YResolution',
30
   'Exif.Image.ResolutionUnit',
31
   'Exif.Image.Software',
32
   'Exif.Image.DateTime',
33
   'Exif.Image.Artist',
34
   'Exif.Image.Copyright',
35
   'Exif.Image.ExifTag',
36
   'Exif.Photo.Flash',
37
   'Exif.Photo.PixelXDimension',
38
   'Exif.Photo.PixelYDimension']
39
40
Each of those tags can be accessed with the ``[]`` operator on the metadata,
41
much like a python dictionary::
42
43
  >>> tag = metadata['Exif.Image.DateTime']
44
45
The value of an :class:`ExifTag` object can be accessed in two different ways:
46
with the :attr:`raw_value` and with the :attr:`value` attributes::
47
48
  >>> tag.raw_value
49
  '2004-07-13T21:23:44Z'
50
51
  >>> tag.value
52
  datetime.datetime(2004, 7, 13, 21, 23, 44)
53
54
The raw value is always a byte string, this is how the value is stored in the
55
file. The value is lazily computed from the raw value depending on the EXIF type
56
of the tag, and is represented as a convenient python object to allow easy
57
manipulation.
58
59
Note that querying the value of a tag may raise an :exc:`ExifValueError` if the
60
format of the raw value is invalid according to the EXIF specification (may
268.1.17 by Olivier Tilloy
Tutorial: reading and writing IPTC tags.
61
happen if it was written by other software that implements the specification in
62
a broken manner), or if pyexiv2 doesn't know how to convert it to a convenient
268.1.16 by Olivier Tilloy
Tutorial: reading and writing EXIF tags.
63
python object.
64
65
Accessing the value of a tag as a python object allows easy manipulation and
66
formatting::
67
68
  >>> tag.value.strftime('%A %d %B %Y, %H:%M:%S')
69
  'Tuesday 13 July 2004, 21:23:44'
70
268.1.18 by Olivier Tilloy
Tutorial: reading and writing XMP tags.
71
Now let's modify the value of the tag and write it back to the file::
268.1.16 by Olivier Tilloy
Tutorial: reading and writing EXIF tags.
72
268.1.17 by Olivier Tilloy
Tutorial: reading and writing IPTC tags.
73
  >>> import datetime
74
  >>> tag.value = datetime.datetime.today()
268.1.16 by Olivier Tilloy
Tutorial: reading and writing EXIF tags.
75
76
  >>> metadata.write()
77
78
Similarly to reading the value of a tag, one can set either the
79
:attr:`raw_value` or the :attr:`value` (which will be automatically converted to
80
a correctly formatted byte string by pyexiv2).
81
82
You can also add new tags to the metadata by providing a valid key and value
268.1.17 by Olivier Tilloy
Tutorial: reading and writing IPTC tags.
83
pair (see exiv2's documentation for a list of valid
84
`EXIF tags <http://exiv2.org/tags.html>`_)::
268.1.16 by Olivier Tilloy
Tutorial: reading and writing EXIF tags.
85
86
  >>> key = 'Exif.Photo.UserComment'
87
  >>> value = 'This is a useful comment.'
88
  >>> metadata[key] = pyexiv2.ExifTag(key, value)
89
275.1.5 by Olivier Tilloy
Tutorial: direct value assignment explained.
90
As a handy shortcut, you can always assign a value for a given key regardless
91
of whether it's already present in the metadata.
92
If a tag was present, its value is overwritten.
93
If the tag was not present, one is created and its value is set::
94
95
  >>> metadata[key] = value
96
332.1.4 by Olivier Tilloy
New section in the tutorial on EXIF thumbnails.
97
The EXIF data may optionally embed a thumbnail in the JPEG or TIFF format.
98
The thumbnail can be accessed, set from a JPEG file or buffer, saved to disk and
99
erased::
100
101
  >>> thumb = metadata.exif_thumbnail
102
  >>> thumb.set_from_file('/tmp/thumbnail.jpg')
103
  >>> thumb.write_to_file('/tmp/copy')
104
  >>> thumb.erase()
105
  >>> metadata.write()
106
268.1.17 by Olivier Tilloy
Tutorial: reading and writing IPTC tags.
107
108
Reading and writing IPTC tags
109
#############################
110
111
Reading and writing IPTC tags works pretty much the same way as with EXIF tags.
112
Let's retrieve the list of all available IPTC tags in the image::
113
114
  >>> metadata.iptc_keys
115
  ['Iptc.Application2.Caption',
116
   'Iptc.Application2.Writer',
117
   'Iptc.Application2.Byline',
118
   'Iptc.Application2.ObjectName',
119
   'Iptc.Application2.DateCreated',
120
   'Iptc.Application2.City',
121
   'Iptc.Application2.ProvinceState',
122
   'Iptc.Application2.CountryName',
123
   'Iptc.Application2.Category',
124
   'Iptc.Application2.Keywords',
125
   'Iptc.Application2.Copyright']
126
127
Each of those tags can be accessed with the ``[]`` operator on the metadata::
128
129
  >>> tag = metadata['Iptc.Application2.DateCreated']
130
131
An IPTC tag always has a list of values rather than a single value.
132
This is because some tags have a repeatable character.
133
Tags that are not repeatable only hold one value in their list of values.
134
135
Check the :attr:`repeatable` attribute to know whether a tag can hold more than
136
one value::
137
138
  >>> tag.repeatable
139
  False
140
141
As with EXIF tags, the values of an :class:`IptcTag` object can be accessed in
331.1.1 by Olivier Tilloy
Rename the .raw_values and .values properties of IPTC tags to .raw_value and .value,
142
two different ways: with the :attr:`raw_value` and with the :attr:`value`
268.1.17 by Olivier Tilloy
Tutorial: reading and writing IPTC tags.
143
attributes::
144
331.1.1 by Olivier Tilloy
Rename the .raw_values and .values properties of IPTC tags to .raw_value and .value,
145
  >>> tag.raw_value
268.1.17 by Olivier Tilloy
Tutorial: reading and writing IPTC tags.
146
  ['2004-07-13']
147
331.1.1 by Olivier Tilloy
Rename the .raw_values and .values properties of IPTC tags to .raw_value and .value,
148
  >>> tag.value
268.1.17 by Olivier Tilloy
Tutorial: reading and writing IPTC tags.
149
  [datetime.date(2004, 7, 13)]
150
268.1.18 by Olivier Tilloy
Tutorial: reading and writing XMP tags.
151
Note that querying the values of a tag may raise an :exc:`IptcValueError` if the
152
format of the raw values is invalid according to the IPTC specification (may
268.1.17 by Olivier Tilloy
Tutorial: reading and writing IPTC tags.
153
happen if it was written by other software that implements the specification in
154
a broken manner), or if pyexiv2 doesn't know how to convert it to a convenient
155
python object.
156
268.1.18 by Olivier Tilloy
Tutorial: reading and writing XMP tags.
157
Now let's modify the values of the tag and write it back to the file::
268.1.17 by Olivier Tilloy
Tutorial: reading and writing IPTC tags.
158
331.1.1 by Olivier Tilloy
Rename the .raw_values and .values properties of IPTC tags to .raw_value and .value,
159
  >>> tag.value = [datetime.date.today()]
268.1.17 by Olivier Tilloy
Tutorial: reading and writing IPTC tags.
160
161
  >>> metadata.write()
162
163
Similarly to reading the values of a tag, one can set either the
331.1.1 by Olivier Tilloy
Rename the .raw_values and .values properties of IPTC tags to .raw_value and .value,
164
:attr:`raw_value` or the :attr:`value` (which will be automatically converted
268.1.17 by Olivier Tilloy
Tutorial: reading and writing IPTC tags.
165
to correctly formatted byte strings by pyexiv2).
166
167
You can also add new tags to the metadata by providing a valid key and values
168
pair (see exiv2's documentation for a list of valid
169
`IPTC tags <http://exiv2.org/iptc.html>`_)::
170
171
  >>> key = 'Iptc.Application2.Contact'
172
  >>> values = ['John', 'Paul', 'Ringo', 'George']
173
  >>> metadata[key] = pyexiv2.IptcTag(key, values)
174
275.1.5 by Olivier Tilloy
Tutorial: direct value assignment explained.
175
As a handy shortcut, you can always assign values for a given key regardless
176
of whether it's already present in the metadata.
177
If a tag was present, its values are overwritten.
178
If the tag was not present, one is created and its values are set::
179
180
  >>> metadata[key] = values
181
344.1.4 by Olivier Tilloy
Added a section on iptc_charset to the tutorial.
182
The IPTC metadata in an image may embed an optional character set for its
183
encoding. This is defined by the ``Iptc.Envelope.CharacterSet`` tag.
184
The :class:`ImageMetadata` class has an :attr:`iptc_charset` property that
185
allows to easily get, set and delete this value::
186
187
  >>> metadata.iptc_charset
188
  'ascii'
189
190
  >>> metadata.iptc_charset = 'utf-8'
191
192
  >>> del metadata.iptc_charset
193
194
Note that at the moment, the only supported charset that can be assigned to the
195
property is ``utf-8``.
196
Also note that even if the charset is not explicitly set, its value may be
197
inferred from the contents of the image. If not, it will be ``None``.
198
268.1.18 by Olivier Tilloy
Tutorial: reading and writing XMP tags.
199
Reading and writing XMP tags
200
############################
201
202
Reading and writing XMP tags works pretty much the same way as with EXIF tags.
203
Let's retrieve the list of all available XMP tags in the image::
204
205
  >>> metadata.xmp_keys
206
  ['Xmp.dc.creator',
207
   'Xmp.dc.description',
208
   'Xmp.dc.rights',
209
   'Xmp.dc.source',
210
   'Xmp.dc.subject',
211
   'Xmp.dc.title',
212
   'Xmp.xmp.CreateDate',
213
   'Xmp.xmp.ModifyDate']
214
215
Each of those tags can be accessed with the ``[]`` operator on the metadata::
216
217
  >>> tag = metadata['Xmp.xmp.ModifyDate']
218
331.1.1 by Olivier Tilloy
Rename the .raw_values and .values properties of IPTC tags to .raw_value and .value,
219
As with EXIF tags, the value of an :class:`XmpTag` object can be accessed in
268.1.18 by Olivier Tilloy
Tutorial: reading and writing XMP tags.
220
two different ways: with the :attr:`raw_value` and with the :attr:`value`
221
attributes::
222
223
  >>> tag.raw_value
224
  '2002-07-19T13:28:10'
225
226
  >>> tag.value
227
  datetime.datetime(2002, 7, 19, 13, 28, 10)
228
229
Note that querying the value of a tag may raise an :exc:`XmpValueError` if the
230
format of the raw value is invalid according to the XMP specification (may
231
happen if it was written by other software that implements the specification in
232
a broken manner), or if pyexiv2 doesn't know how to convert it to a convenient
233
python object.
234
235
Now let's modify the value of the tag and write it back to the file::
236
237
  >>> tag.value = datetime.datetime.today()
238
239
  >>> metadata.write()
240
241
Similarly to reading the value of a tag, one can set either the
242
:attr:`raw_value` or the :attr:`value` (which will be automatically converted to
243
a correctly formatted byte string by pyexiv2).
244
245
You can also add new tags to the metadata by providing a valid key and value
246
pair (see exiv2's documentation for a list of valid
247
`XMP tags <http://exiv2.org/tags-xmp-dc.html>`_)::
248
249
  >>> key = 'Xmp.xmp.Label'
250
  >>> value = 'A beautiful picture.'
251
  >>> metadata[key] = pyexiv2.XmpTag(key, value)
252
275.1.5 by Olivier Tilloy
Tutorial: direct value assignment explained.
253
As a handy shortcut, you can always assign a value for a given key regardless
254
of whether it's already present in the metadata.
255
If a tag was present, its value is overwritten.
256
If the tag was not present, one is created and its value is set::
257
258
  >>> metadata[key] = value
259
334.2.6 by Olivier Tilloy
Added a section on custom XMP namespaces to the tutorial.
260
If you need to write custom metadata, you can register a custom XMP namespace::
261
262
  >>> pyexiv2.xmp.register_namespace('http://example.org/foo/', 'foo')
263
  >>> metadata['Xmp.foo.bar'] = 'baz'
264
265
Note that a limitation of the current implementation is that only simple text
266
values can be written to tags in a custom namespace.
267
268
A custom namespace can be unregistered. This has the effect of invalidating all
269
tags in this namespace for images that have not been written back yet::
270
271
  >>> pyexiv2.xmp.unregister_namespace('http://example.org/foo/')
272
268.1.19 by Olivier Tilloy
Tutorial: accessing embedded previews.
273
Accessing embedded previews
274
###########################
275
276
Images may embed previews (also called thumbnails) of various sizes in their
277
metadata. pyexiv2 allows to easily access them::
278
279
  >>> previews = metadata.previews
280
281
  >>> len(previews)
282
  2
283
284
They are sorted by increasing size. Let's play with the largest one::
285
286
  >>> largest = previews[-1]
287
288
  >>> largest.dimensions
289
  (320, 240)
290
291
  >>> largest.mime_type
292
  'image/jpeg'
293
294
  >>> largest.write_to_file('largest')
295