This file is indexed.

/usr/share/doc/python-pyexiv2-doc/tutorial.html is in python-pyexiv2-doc 0.3.2-5ubuntu4build6.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Tutorial &#8212; pyexiv2 0.3.2 documentation</title>
    <link rel="stylesheet" href="_static/classic.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '0.3.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="shortcut icon" href="_static/pyexiv2.ico"/>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="API documentation" href="api.html" />
    <link rel="prev" title="pyexiv2 documentation" href="index.html" /> 
  </head>
  <body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="api.html" title="API documentation"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="index.html" title="pyexiv2 documentation"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">pyexiv2 0.3.2 documentation</a> &#187;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="tutorial">
<h1>Tutorial<a class="headerlink" href="#tutorial" title="Permalink to this headline"></a></h1>
<p>This tutorial is meant to give you a quick overview of what pyexiv2 allows you
to do. You can just read it through or follow it interactively, in which case
you will need to have pyexiv2 installed.
It doesn’t cover all the possibilities offered by pyexiv2, only a basic subset
of them. For complete reference, see the <a class="reference internal" href="api.html"><span class="doc">API documentation</span></a>.</p>
<p>Let’s get started!</p>
<p>First of all, we import the pyexiv2 module:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">pyexiv2</span>
</pre></div>
</div>
<p>We then load an image file and read its metadata:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">metadata</span> <span class="o">=</span> <span class="n">pyexiv2</span><span class="o">.</span><span class="n">ImageMetadata</span><span class="p">(</span><span class="s1">&#39;test.jpg&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">metadata</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
</pre></div>
</div>
<div class="section" id="reading-and-writing-exif-tags">
<h2>Reading and writing EXIF tags<a class="headerlink" href="#reading-and-writing-exif-tags" title="Permalink to this headline"></a></h2>
<p>Let’s retrieve a list of all the available EXIF tags available in the image:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">metadata</span><span class="o">.</span><span class="n">exif_keys</span>
<span class="go">[&#39;Exif.Image.ImageDescription&#39;,</span>
<span class="go"> &#39;Exif.Image.XResolution&#39;,</span>
<span class="go"> &#39;Exif.Image.YResolution&#39;,</span>
<span class="go"> &#39;Exif.Image.ResolutionUnit&#39;,</span>
<span class="go"> &#39;Exif.Image.Software&#39;,</span>
<span class="go"> &#39;Exif.Image.DateTime&#39;,</span>
<span class="go"> &#39;Exif.Image.Artist&#39;,</span>
<span class="go"> &#39;Exif.Image.Copyright&#39;,</span>
<span class="go"> &#39;Exif.Image.ExifTag&#39;,</span>
<span class="go"> &#39;Exif.Photo.Flash&#39;,</span>
<span class="go"> &#39;Exif.Photo.PixelXDimension&#39;,</span>
<span class="go"> &#39;Exif.Photo.PixelYDimension&#39;]</span>
</pre></div>
</div>
<p>Each of those tags can be accessed with the <code class="docutils literal"><span class="pre">[]</span></code> operator on the metadata,
much like a python dictionary:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">tag</span> <span class="o">=</span> <span class="n">metadata</span><span class="p">[</span><span class="s1">&#39;Exif.Image.DateTime&#39;</span><span class="p">]</span>
</pre></div>
</div>
<p>The value of an <code class="xref py py-class docutils literal"><span class="pre">ExifTag</span></code> object can be accessed in two different ways:
with the <code class="xref py py-attr docutils literal"><span class="pre">raw_value</span></code> and with the <code class="xref py py-attr docutils literal"><span class="pre">value</span></code> attributes:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">tag</span><span class="o">.</span><span class="n">raw_value</span>
<span class="go">&#39;2004-07-13T21:23:44Z&#39;</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">tag</span><span class="o">.</span><span class="n">value</span>
<span class="go">datetime.datetime(2004, 7, 13, 21, 23, 44)</span>
</pre></div>
</div>
<p>The raw value is always a byte string, this is how the value is stored in the
file. The value is lazily computed from the raw value depending on the EXIF type
of the tag, and is represented as a convenient python object to allow easy
manipulation.</p>
<p>Note that querying the value of a tag may raise an <code class="xref py py-exc docutils literal"><span class="pre">ExifValueError</span></code> if the
format of the raw value is invalid according to the EXIF specification (may
happen if it was written by other software that implements the specification in
a broken manner), or if pyexiv2 doesn’t know how to convert it to a convenient
python object.</p>
<p>Accessing the value of a tag as a python object allows easy manipulation and
formatting:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">tag</span><span class="o">.</span><span class="n">value</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s1">&#39;%A </span><span class="si">%d</span><span class="s1"> %B %Y, %H:%M:%S&#39;</span><span class="p">)</span>
<span class="go">&#39;Tuesday 13 July 2004, 21:23:44&#39;</span>
</pre></div>
</div>
<p>Now let’s modify the value of the tag and write it back to the file:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">datetime</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">tag</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">today</span><span class="p">()</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">metadata</span><span class="o">.</span><span class="n">write</span><span class="p">()</span>
</pre></div>
</div>
<p>Similarly to reading the value of a tag, one can set either the
<code class="xref py py-attr docutils literal"><span class="pre">raw_value</span></code> or the <code class="xref py py-attr docutils literal"><span class="pre">value</span></code> (which will be automatically converted to
a correctly formatted byte string by pyexiv2).</p>
<p>You can also add new tags to the metadata by providing a valid key and value
pair (see exiv2’s documentation for a list of valid
<a class="reference external" href="http://exiv2.org/tags.html">EXIF tags</a>):</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">key</span> <span class="o">=</span> <span class="s1">&#39;Exif.Photo.UserComment&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">value</span> <span class="o">=</span> <span class="s1">&#39;This is a useful comment.&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">metadata</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">pyexiv2</span><span class="o">.</span><span class="n">ExifTag</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
</pre></div>
</div>
<p>As a handy shortcut, you can always assign a value for a given key regardless
of whether it’s already present in the metadata.
If a tag was present, its value is overwritten.
If the tag was not present, one is created and its value is set:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">metadata</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
</pre></div>
</div>
<p>The EXIF data may optionally embed a thumbnail in the JPEG or TIFF format.
The thumbnail can be accessed, set from a JPEG file or buffer, saved to disk and
erased:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">thumb</span> <span class="o">=</span> <span class="n">metadata</span><span class="o">.</span><span class="n">exif_thumbnail</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">thumb</span><span class="o">.</span><span class="n">set_from_file</span><span class="p">(</span><span class="s1">&#39;/tmp/thumbnail.jpg&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">thumb</span><span class="o">.</span><span class="n">write_to_file</span><span class="p">(</span><span class="s1">&#39;/tmp/copy&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">thumb</span><span class="o">.</span><span class="n">erase</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">metadata</span><span class="o">.</span><span class="n">write</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="reading-and-writing-iptc-tags">
<h2>Reading and writing IPTC tags<a class="headerlink" href="#reading-and-writing-iptc-tags" title="Permalink to this headline"></a></h2>
<p>Reading and writing IPTC tags works pretty much the same way as with EXIF tags.
Let’s retrieve the list of all available IPTC tags in the image:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">metadata</span><span class="o">.</span><span class="n">iptc_keys</span>
<span class="go">[&#39;Iptc.Application2.Caption&#39;,</span>
<span class="go"> &#39;Iptc.Application2.Writer&#39;,</span>
<span class="go"> &#39;Iptc.Application2.Byline&#39;,</span>
<span class="go"> &#39;Iptc.Application2.ObjectName&#39;,</span>
<span class="go"> &#39;Iptc.Application2.DateCreated&#39;,</span>
<span class="go"> &#39;Iptc.Application2.City&#39;,</span>
<span class="go"> &#39;Iptc.Application2.ProvinceState&#39;,</span>
<span class="go"> &#39;Iptc.Application2.CountryName&#39;,</span>
<span class="go"> &#39;Iptc.Application2.Category&#39;,</span>
<span class="go"> &#39;Iptc.Application2.Keywords&#39;,</span>
<span class="go"> &#39;Iptc.Application2.Copyright&#39;]</span>
</pre></div>
</div>
<p>Each of those tags can be accessed with the <code class="docutils literal"><span class="pre">[]</span></code> operator on the metadata:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">tag</span> <span class="o">=</span> <span class="n">metadata</span><span class="p">[</span><span class="s1">&#39;Iptc.Application2.DateCreated&#39;</span><span class="p">]</span>
</pre></div>
</div>
<p>An IPTC tag always has a list of values rather than a single value.
This is because some tags have a repeatable character.
Tags that are not repeatable only hold one value in their list of values.</p>
<p>Check the <code class="xref py py-attr docutils literal"><span class="pre">repeatable</span></code> attribute to know whether a tag can hold more than
one value:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">tag</span><span class="o">.</span><span class="n">repeatable</span>
<span class="go">False</span>
</pre></div>
</div>
<p>As with EXIF tags, the values of an <code class="xref py py-class docutils literal"><span class="pre">IptcTag</span></code> object can be accessed in
two different ways: with the <code class="xref py py-attr docutils literal"><span class="pre">raw_value</span></code> and with the <code class="xref py py-attr docutils literal"><span class="pre">value</span></code>
attributes:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">tag</span><span class="o">.</span><span class="n">raw_value</span>
<span class="go">[&#39;2004-07-13&#39;]</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">tag</span><span class="o">.</span><span class="n">value</span>
<span class="go">[datetime.date(2004, 7, 13)]</span>
</pre></div>
</div>
<p>Note that querying the values of a tag may raise an <code class="xref py py-exc docutils literal"><span class="pre">IptcValueError</span></code> if the
format of the raw values is invalid according to the IPTC specification (may
happen if it was written by other software that implements the specification in
a broken manner), or if pyexiv2 doesn’t know how to convert it to a convenient
python object.</p>
<p>Now let’s modify the values of the tag and write it back to the file:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">tag</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="p">[</span><span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">today</span><span class="p">()]</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">metadata</span><span class="o">.</span><span class="n">write</span><span class="p">()</span>
</pre></div>
</div>
<p>Similarly to reading the values of a tag, one can set either the
<code class="xref py py-attr docutils literal"><span class="pre">raw_value</span></code> or the <code class="xref py py-attr docutils literal"><span class="pre">value</span></code> (which will be automatically converted
to correctly formatted byte strings by pyexiv2).</p>
<p>You can also add new tags to the metadata by providing a valid key and values
pair (see exiv2’s documentation for a list of valid
<a class="reference external" href="http://exiv2.org/iptc.html">IPTC tags</a>):</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">key</span> <span class="o">=</span> <span class="s1">&#39;Iptc.Application2.Contact&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">values</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;John&#39;</span><span class="p">,</span> <span class="s1">&#39;Paul&#39;</span><span class="p">,</span> <span class="s1">&#39;Ringo&#39;</span><span class="p">,</span> <span class="s1">&#39;George&#39;</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">metadata</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">pyexiv2</span><span class="o">.</span><span class="n">IptcTag</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">values</span><span class="p">)</span>
</pre></div>
</div>
<p>As a handy shortcut, you can always assign values for a given key regardless
of whether it’s already present in the metadata.
If a tag was present, its values are overwritten.
If the tag was not present, one is created and its values are set:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">metadata</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">values</span>
</pre></div>
</div>
<p>The IPTC metadata in an image may embed an optional character set for its
encoding. This is defined by the <code class="docutils literal"><span class="pre">Iptc.Envelope.CharacterSet</span></code> tag.
The <code class="xref py py-class docutils literal"><span class="pre">ImageMetadata</span></code> class has an <code class="xref py py-attr docutils literal"><span class="pre">iptc_charset</span></code> property that
allows to easily get, set and delete this value:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">metadata</span><span class="o">.</span><span class="n">iptc_charset</span>
<span class="go">&#39;ascii&#39;</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">metadata</span><span class="o">.</span><span class="n">iptc_charset</span> <span class="o">=</span> <span class="s1">&#39;utf-8&#39;</span>

<span class="gp">&gt;&gt;&gt; </span><span class="k">del</span> <span class="n">metadata</span><span class="o">.</span><span class="n">iptc_charset</span>
</pre></div>
</div>
<p>Note that at the moment, the only supported charset that can be assigned to the
property is <code class="docutils literal"><span class="pre">utf-8</span></code>.
Also note that even if the charset is not explicitly set, its value may be
inferred from the contents of the image. If not, it will be <code class="docutils literal"><span class="pre">None</span></code>.</p>
</div>
<div class="section" id="reading-and-writing-xmp-tags">
<h2>Reading and writing XMP tags<a class="headerlink" href="#reading-and-writing-xmp-tags" title="Permalink to this headline"></a></h2>
<p>Reading and writing XMP tags works pretty much the same way as with EXIF tags.
Let’s retrieve the list of all available XMP tags in the image:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">metadata</span><span class="o">.</span><span class="n">xmp_keys</span>
<span class="go">[&#39;Xmp.dc.creator&#39;,</span>
<span class="go"> &#39;Xmp.dc.description&#39;,</span>
<span class="go"> &#39;Xmp.dc.rights&#39;,</span>
<span class="go"> &#39;Xmp.dc.source&#39;,</span>
<span class="go"> &#39;Xmp.dc.subject&#39;,</span>
<span class="go"> &#39;Xmp.dc.title&#39;,</span>
<span class="go"> &#39;Xmp.xmp.CreateDate&#39;,</span>
<span class="go"> &#39;Xmp.xmp.ModifyDate&#39;]</span>
</pre></div>
</div>
<p>Each of those tags can be accessed with the <code class="docutils literal"><span class="pre">[]</span></code> operator on the metadata:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">tag</span> <span class="o">=</span> <span class="n">metadata</span><span class="p">[</span><span class="s1">&#39;Xmp.xmp.ModifyDate&#39;</span><span class="p">]</span>
</pre></div>
</div>
<p>As with EXIF tags, the value of an <code class="xref py py-class docutils literal"><span class="pre">XmpTag</span></code> object can be accessed in
two different ways: with the <code class="xref py py-attr docutils literal"><span class="pre">raw_value</span></code> and with the <code class="xref py py-attr docutils literal"><span class="pre">value</span></code>
attributes:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">tag</span><span class="o">.</span><span class="n">raw_value</span>
<span class="go">&#39;2002-07-19T13:28:10&#39;</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">tag</span><span class="o">.</span><span class="n">value</span>
<span class="go">datetime.datetime(2002, 7, 19, 13, 28, 10)</span>
</pre></div>
</div>
<p>Note that querying the value of a tag may raise an <code class="xref py py-exc docutils literal"><span class="pre">XmpValueError</span></code> if the
format of the raw value is invalid according to the XMP specification (may
happen if it was written by other software that implements the specification in
a broken manner), or if pyexiv2 doesn’t know how to convert it to a convenient
python object.</p>
<p>Now let’s modify the value of the tag and write it back to the file:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">tag</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">today</span><span class="p">()</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">metadata</span><span class="o">.</span><span class="n">write</span><span class="p">()</span>
</pre></div>
</div>
<p>Similarly to reading the value of a tag, one can set either the
<code class="xref py py-attr docutils literal"><span class="pre">raw_value</span></code> or the <code class="xref py py-attr docutils literal"><span class="pre">value</span></code> (which will be automatically converted to
a correctly formatted byte string by pyexiv2).</p>
<p>You can also add new tags to the metadata by providing a valid key and value
pair (see exiv2’s documentation for a list of valid
<a class="reference external" href="http://exiv2.org/tags-xmp-dc.html">XMP tags</a>):</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">key</span> <span class="o">=</span> <span class="s1">&#39;Xmp.xmp.Label&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">value</span> <span class="o">=</span> <span class="s1">&#39;A beautiful picture.&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">metadata</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">pyexiv2</span><span class="o">.</span><span class="n">XmpTag</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
</pre></div>
</div>
<p>As a handy shortcut, you can always assign a value for a given key regardless
of whether it’s already present in the metadata.
If a tag was present, its value is overwritten.
If the tag was not present, one is created and its value is set:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">metadata</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
</pre></div>
</div>
<p>If you need to write custom metadata, you can register a custom XMP namespace:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pyexiv2</span><span class="o">.</span><span class="n">xmp</span><span class="o">.</span><span class="n">register_namespace</span><span class="p">(</span><span class="s1">&#39;http://example.org/foo/&#39;</span><span class="p">,</span> <span class="s1">&#39;foo&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">metadata</span><span class="p">[</span><span class="s1">&#39;Xmp.foo.bar&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;baz&#39;</span>
</pre></div>
</div>
<p>Note that a limitation of the current implementation is that only simple text
values can be written to tags in a custom namespace.</p>
<p>A custom namespace can be unregistered. This has the effect of invalidating all
tags in this namespace for images that have not been written back yet:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">pyexiv2</span><span class="o">.</span><span class="n">xmp</span><span class="o">.</span><span class="n">unregister_namespace</span><span class="p">(</span><span class="s1">&#39;http://example.org/foo/&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="accessing-embedded-previews">
<h2>Accessing embedded previews<a class="headerlink" href="#accessing-embedded-previews" title="Permalink to this headline"></a></h2>
<p>Images may embed previews (also called thumbnails) of various sizes in their
metadata. pyexiv2 allows to easily access them:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">previews</span> <span class="o">=</span> <span class="n">metadata</span><span class="o">.</span><span class="n">previews</span>

<span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">previews</span><span class="p">)</span>
<span class="go">2</span>
</pre></div>
</div>
<p>They are sorted by increasing size. Let’s play with the largest one:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">largest</span> <span class="o">=</span> <span class="n">previews</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">largest</span><span class="o">.</span><span class="n">dimensions</span>
<span class="go">(320, 240)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">largest</span><span class="o">.</span><span class="n">mime_type</span>
<span class="go">&#39;image/jpeg&#39;</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">largest</span><span class="o">.</span><span class="n">write_to_file</span><span class="p">(</span><span class="s1">&#39;largest&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="index.html">
              <img class="logo" src="_static/pyexiv2-big-192x192.png" alt="Logo"/>
            </a></p>
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Tutorial</a><ul>
<li><a class="reference internal" href="#reading-and-writing-exif-tags">Reading and writing EXIF tags</a></li>
<li><a class="reference internal" href="#reading-and-writing-iptc-tags">Reading and writing IPTC tags</a></li>
<li><a class="reference internal" href="#reading-and-writing-xmp-tags">Reading and writing XMP tags</a></li>
<li><a class="reference internal" href="#accessing-embedded-previews">Accessing embedded previews</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="index.html"
                        title="previous chapter">pyexiv2 documentation</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="api.html"
                        title="next chapter">API documentation</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="_sources/tutorial.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="api.html" title="API documentation"
             >next</a> |</li>
        <li class="right" >
          <a href="index.html" title="pyexiv2 documentation"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">pyexiv2 0.3.2 documentation</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2006-2017, Olivier Tilloy.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.6.5.
    </div>
  </body>
</html>