Debian Codegraph @

html/struct.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
2 "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
3 <html>
4 <head>
5 <title>Structured Information</title>
6 <link rel=stylesheet type='text/css' href='style.css' title='Style'>
7 <style type="text/css">
8 <!--
9 pre { padding: 0; margin: 0px 2px }
10 spc { margin-top: 5em }
11 li { margin-top: .5em }
12 li.c { margin-top: 0 }
13 -->
14 </style>
15 </head>
16 <body>
17 <div class='index'>
18 <a href="#Reading">Reading</a>
19 <br><a href="#Writing">Writing</a>
20 <br><a href="#Copying">Copying</a>
21 <br><a href="#Deleting">Deleting</a>
22 <br><a href="#Fields">Field Names</a>
23 <br><a href="#Serialize">Serialization</a>
24 <br><a href="#Examples">Examples</a>
25 <br><a href="#User">User-Defined</a>
26 </div>
27 <h1 class='up'>Structured Information</h1>
28
29 <p>ExifTool has the ability to read and write XMP structures through the use of
30 either <b>structured or flattened tags</b>. The ability to write via structured
31 input was added in ExifTool version 8.44; older versions accepted only flattened
32 tags as input.</p>
33
34 <p>To illustrate the concept of a flattened tag, the <code>XMP-exif:Flash</code>
35 structure contains <code>Fired</code> and <code>Mode</code> fields (among
36 others). The flattened tags corresponding to these structure fields are
37 <code>XMP-exif:FlashFired</code> and <code>XMP-exif:FlashMode</code>. In the
38 <a href="TagNames/XMP.html">XMP Tags documentation</a>, flattened tags are
39 indicated by an underline (<code>_</code>) after the Writable type.</p>
40
41 <p>This page describes various techniques used to read and write XMP structures
42 using both structured and flattened tags.</p>
43
44 <a name='Reading'></a>
45 <h2>Reading</h2>
46
47 <p>When reading, structures are <b>flattened by default</b>, and ExifTool
48 returns one "flattened" tag for each field in the structure:</p>
49
50 <blockquote><table class='box'><tr><td>
51 <pre>&gt; <span class=code>exiftool -xmp:all a.xmp</span>
52 XMP Toolkit : Image::ExifTool 8.44
53 Flash Fired : True
54 Flash Mode : On
55 Flash Return : Return not detected</pre>
56 </td></tr></table></blockquote>
57
58 <p>But the <code>-struct</code> option may be used to give <b>structured
59 output</b>. In this mode structures are returned instead of separate
60 "flattened" tags:</p>
61
62 <blockquote><table class='box'><tr><td>
63 <pre>&gt; <span class=code>exiftool -struct -xmp:all a.xmp</span>
64 XMP Toolkit : Image::ExifTool 8.44
65 Flash : {Fired=True,Mode=On,Return=Return not detected}</pre>
66 </td></tr></table></blockquote>
67
68 <p class=lt>(Note: As illustrated in the example above, structures are
69 <a href='#Serialize'>serialized</a> for console output by the ExifTool
70 application. However, via the API with the <code>Struct</code> option,
71 they are returned as Perl HASH references.)</p>
72
73 <p>The <code>-struct</code> option may also be combined with the JSON
74 (<code>-j</code>), PHP (<code>-php</code>) or XML (<code>-X</code>) output
75 formats to provide a structured format which may be more compatible with other
76 applications.</p>
77
78 <a name='Writing'></a>
79 <h2>Writing</h2>
80
81 <p>When writing, <b>flattened tags and structures may be used interchangeably</b>.
82 For example, the following commands all have the same effect.</p>
83
84 <blockquote><table class='box'><tr><td>
85 <pre class=code>exiftool -flashmode=on -flashreturn=not -flashfired=true a.xmp
86 exiftool -xmp:flash="{mode=on,fired=true}" -flashreturn=not a.xmp
87 exiftool -xmp:flash="{mode=on,fired=true,return=not}" a.xmp</pre>
88 </td></tr></table></blockquote>
89
90 <p class=lt>(Note: Structures must be <a href='#Serialize'>serialized</a> when
91 writing via the command-line application, in the same format as when reading with
92 the <code>-struct</code> option.)</p>
93
94 <p>An advantage of writing in structured form is that it can be easier to
95 achieve the desired hierarchy with complex structures or when there are multiple
96 structures in a list. For example, this command adds a new hierarchical keyword
97 to the XMP-mwg-kw:HierarchicalKeywords list:</p>
98
99 <blockquote><table class='box'><tr><td>
100 <pre class=code>exiftool -hierarchicalkeywords+="{keyword=cat,children={keyword=Siamese}}" a.jpg</pre>
101 </td></tr></table></blockquote>
102
103 <p>But the flattened tags may be more convenient for <b>adding or replacing a
104 single field</b> in an existing structure because writing as a structure would
105 require that the entire structure be replaced. For example, the following command
106 adds a new second-level keyword to an existing HierarchicalKeywords structure:</p>
107
108 <blockquote><table class='box'><tr><td>
109 <pre class=code>exiftool -hierarchicalkeywords2+="Persian" a.jpg</pre>
110 </td></tr></table></blockquote>
111
112 <p><b>Tricky:</b> There is one drawback when using this technique to add new
113 fields to existing structures in lists: New fields are added to the first
114 structure which doesn't already contain the corresponding field. So before
115 adding a new field to a arbitrary structure, dummy fields must first be added to
116 all earlier structures in the list which are missing this field. However, the
117 alternative of adding a new field by writing structured information also has its
118 drawbacks. Here, although a specific structure in a list can easily be targeted
119 through any unique combination of field values, the drawback is that the entire
120 structure must be replaced (see <a href="#Deleting">Deleting / Replacing</a>
121 below).</p>
122
123 <p>The flattened tag names may also be used to <b>write structures at any level
124 in a complex hierarchy</b>. The following example writes a third-level
125 structure inside a HierarchicalKeywords structure:</p>
126
127 <blockquote><table class='box'><tr><td>
128 <pre class=code>exiftool -hierarchicalkeywords2Children='{Keyword=Tabby,Applied=true}' a.jpg</pre>
129 </td></tr></table></blockquote>
130
131 <p class=lt>(Note: Containing structures are created as necessary. In this
132 case, the HierarchicalKeywords and top-level KeywordInfo structures would be
133 created if they didn't already exist.)</p>
134
135 <p>The <b>order of structure fields is not significant</b>, so they may be read
136 in a different order than written, unlike arrays which maintain the same order.
137 To give a predictable output, fields in structured information are <b>sorted in
138 alphabetical order</b> of field name by ExifTool when reading and writing.</p>
139
140 <p>If there are <b>errors converting some fields</b> of the input structure,
141 other fields are still written and a warning is issued (but only one warning per
142 structure is reported). This also applies when copying structured information
143 except that the <code>-v3</code> option must be used to see the warnings when
144 copying.</p>
145
146 <p><b>Programmers:</b> Structured information is written and read as Perl HASH
147 references via the ExifTool API, but it may also be written as a serialized
148 string. The following two techniques are equivalent:</p>
149
150 <blockquote><table class='box'><tr><td>
151 <pre># as a HASH reference
152 <span class=code>$exifTool-&gt;SetNewValue('XMP:Flash' =&gt; { mode=>'on', fired=>'true', return=>'not' });
153 </span>
154 # as a serialized string
155 <span class=code>$exifTool-&gt;SetNewValue('XMP:Flash' =&gt; '{mode=on,fired=true,return=not}');</span></pre>
156 </td></tr></table></blockquote>
157
158 <a name='Copying'></a>
159 <h2>Copying</h2>
160
161 <p>By default, tags are <b>copied as structures</b>, but flattened tag names
162 may still be copied by specifying them explicitly. <span class=lt>(Flattened
163 tags are treated as "unsafe" for copying so they are not copied by default
164 unless the Struct feature is disabled; see below.)</span> Copying as structures
165 allows the hierarchy of complex structures to be preserved.</p>
166
167 <blockquote><table class='box'><tr><td>
168 <pre># this copies all XMP information as structures
169 # (flattened tags are not copied by default...)
170 <span class=code>exiftool -tagsfromfile src.jpg -xmp:all dst.jpg
171 </span>
172 # ... but flattened tags may be copied individually. Here the
173 # first level hierarchical keywords are copied to the Subject tag
174 # (this may be done in the same command as one that copies structures)
175 <span class=code>exiftool -tagsfromfile src.jpg "-subject&lt;hierarchicalkeywords1" dst.jpg</span></pre>
176 </td></tr></table></blockquote>
177
178 <p>Note that when copying a specific structure, only the top-level structures
179 may be specified:</p>
180
181 <blockquote><table class='box'><tr><td>
182 <pre># this copies the complete keyword hierarchy
183 <span class=code>exiftool -tagsfromfile src.jpg -keywordinfo dst.jpg
184 </span>
185 # WRONG because HierarchicalKeywords is NOT a top-level structure!
186 <span class=code>exiftool -tagsfromfile src.jpg -hierarchicalkeywords dst.jpg</span></pre>
187 </td></tr></table></blockquote>
188
189 <p>The copy-as-structure feature may be disabled with <code>--struct</code> on
190 the command line, or by setting the <a href="ExifTool.html#Struct">Struct option</a>
191 to 0 via the API. When this is done, only flattened tags are copied, and
192 structures may not be specified. Conversely, if the structure option is enabled
193 (by setting the <a href="ExifTool.html#Struct">Struct option</a> to 1 via the API,
194 or with <code>-struct</code> on the command line), only structures are copied,
195 and flattened tags may not be specified.</p>
196
197 <p class=lt>(Note: ExifTool 8.43 and earlier copied as flattened tags only, but
198 copying as structures has been the default since the ability to write structured
199 information was introduced in version 8.44. An enhancement in version 8.82
200 allowed flattened tags to be copied explicitly without the need to disable the
201 Struct option.)</p>
202
203 <a name='Deleting'></a>
204 <h2>Deleting / Replacing</h2>
205
206 <p>A <b>complete structure is deleted</b> by specifying one or more matching
207 fields. All fields must match for the structure to be deleted. For example,
208 the following command deletes all HierarchicalKeywords structures which have the
209 Keyword "<code>Terrier</code>" at the second level:</p>
210
211 <blockquote><table class='box'><tr><td>
212 <pre class=code>exiftool -hierarchicalkeywords-="{Children={Keyword=Terrier}}" a.jpg</pre>
213 </td></tr></table></blockquote>
214
215 <p>Structure <b>fields may also be deleted individually</b> using the flattened
216 tag names. The following command deletes only the matching fields from the
217 second-level of all HierarchicalKeywords structures:</p>
218
219 <blockquote><table class='box'><tr><td>
220 <pre class=code>exiftool -hierarchicalkeywords2-="Terrier" a.jpg</pre>
221 </td></tr></table></blockquote>
222
223 <p>Individual structure <b>fields may NOT be deleted by writing a structure</b>
224 with an empty field. Instead, a command like this overwrites the entire
225 structure with a new structure containing an empty field:</p>
226
227 <blockquote><table class='box'><tr><td>
228 <pre><span class=code>exiftool -CreatorContactInfo="{CiAdrCity=}" a.jpg</span> # WRONG!</pre>
229 </td></tr></table></blockquote>
230
231 <p>When deleting and adding back items in lists in the same command, new items
232 are inserted at the point in the list where the first item was removed, or at
233 the end of the list if no items were deleted. This applies to lists of
234 structures as well as simple lists of string values, and provides a mechanism to
235 replace a specific structure or field.</p>
236
237 <a name='Fields'></a>
238 <h2>Field Names</h2>
239
240 <p>Structure field names use a format <b>very similar to tag names</b> in
241 ExifTool. The following table lists some similarities and differences
242 between tag names and structure field names:</p>
243
244 <blockquote><table border=1 cellspacing=0 cellpadding=4 width='100%'>
245 <tr><th>Feature</th><th>Example</th><th>Tag Names</th><th>Field Names</th></tr>
246 <tr align=center><td align=left>Case Insensitivity</td><td>Title, title, TITLE</td>
247 <td class=grn>Yes</td><td class=grn>Yes</td></tr>
248 <tr align=center><td align=left>Alternate Language Suffix</td><td>Title-de</td>
249 <td class=grn>Yes</td><td class=grn>Yes</td></tr>
250 <tr align=center><td align=left>Numerical Value Suffix</td><td>Mode#</td>
251 <td class=grn>Yes</td><td class=grn>Yes</td></tr>
252 <tr align=center><td align=left>Group Name Prefix</td><td>XMP-dc:Title</td>
253 <td class=grn>Yes</td><td class=red>No<sup>&dagger;</sup></td></tr>
254 </table>
255 <sup class=red>&dagger;</sup> Except that group name prefixes are
256 allowed in structures which support arbitrary XMP fields (eg.
257 <a href='TagNames/MWG.html#Extensions'>Region Extensions</a>)
258 </blockquote>
259
260 <a name='Serialize'></a>
261 <h2>Serialization</h2>
262
263 <p>Structures are <b>serialized when reading or writing from the command line</b>.
264 However, serialization is not done when reading via the API, and is optional
265 when writing via the API. The default serialization algorithm is outlined
266 below, but note that ExifTool 12.64 has an
267 <a href="ExifTool.html#StructFormat">API StructFormat option</a> to allow
268 JSON-format serialized structures.</p>
269
270 <p><u>Default serialization algorithm</u></p>
271
272 <ol>
273 <li>Escape the following characters in string values (structure field values and
274 list items) by adding a leading pipe symbol (<code>|</code>):
275 <ul>
276 <li class=c>pipe symbols (<code>|</code>) and commas (<code>,</code>) anywhere in the string</li>
277 <li class=c>closing curly brackets (<code>}</code>) anywhere in structure field values</li>
278 <li class=c>closing square brackets (<code>]</code>) anywhere in list items</li>
279 <li class=c>an opening curly (<code>{</code>) or square (<code>[</code>)
280 bracket, or whitespace character (SPACE, TAB, CR or LF) if it appears at the
281 beginning of the string</li>
282 </ul>
283 <span class=lt>(Note: Any other character may be escaped by adding
284 a leading pipe symbol without effect.)</span></li>
285 <li>Enclose structures in curly brackets. Use an equal sign (<code>=</code>)
286 to separate field names from their corresponding values, and a comma between
287 structure fields.</li>
288 <li>Enclose lists in square brackets, with a comma between list items.</li>
289 <li>Optional whitespace padding may be added anywhere except inside a structure
290 field name, or inside or after a string value, and an optional comma may be
291 added after the last field in a structure.</li>
292 </ol>
293
294 <p>For example, with this command:</p>
295
296 <blockquote><table class='box'><tr><td>
297 <pre class=code>exiftool "-RegionInfo&lt;=INFILE" a.xmp</pre>
298 </td></tr></table></blockquote>
299
300 <p>and the INFILE below, structured information is written to XMP-mwg-rs:RegionInfo.</p>
301
302 <blockquote><table class='box'><tr><td>
303 <pre>{
304 AppliedToDimensions =
305 {
306 W = 4288,
307 H = 2848,
308 Unit = pixel,
309 },
310 RegionList =
311 [
312 {
313 Area =
314 {
315 W = 0.15, H = 0.17, X = 0.3, Y = 0.4,
316 Unit = normalized,
317 },
318 Description = A Physics Icon {relatively speaking|},
319 Name = Albert Einstein,
320 Type = Face,
321 Extensions = {
322 XMP-xmpRights:UsageTerms = copyright Phil Harvey,
323 XMP-xmpRights:UsageTerms-fr = droit d'auteur Phil Harvey,
324 },
325 SeeAlso = dc:subject,
326 },
327 {
328 Area =
329 {
330 W = 0.06, H = 0.09, X = 0.5, Y = 0.6,
331 Unit = normalized,
332 },
333 Description = this is a test|, what did you expect?,
334 Type = Focus,
335 FocusUsage = Evaluated|, Used,
336 }
337 ],
338 }</pre>
339 </td></tr></table></blockquote>
340
341 <p>In this example, white space has been added in all allowed locations for
342 demonstration purposes and to improve readability. Also, optional commas have
343 been added after the last field of each structure. (Note that a comma may NOT
344 be added after the last item in a list because this would be interpreted as an
345 additional list item consisting of a zero-length string.)</p>
346
347 <a name='Examples'></a>
348 <h2>Examples</h2>
349
350 <p>Here is an example of an advanced console session showing some commands which
351 manipulate a complex list of structures (see the
352 <a href="TagNames/XMP.html#iptcExt">XMP-iptcExt tag documentation</a> for details
353 about the ArtworkOrObject structure tags used):</p>
354
355 <blockquote><table class='box'><tr><td>
356 <pre><span class=lt># 1. Create a XMP-iptcExt:ArtworkOrObject structure using flattened tags</span>
357 &gt; <span class=code>exiftool -artworktitle="a title" a.xmp</span>
358 1 image files created
359
360 <span class=lt># -- Read back as flattened tags (-S is used just to shorten the output)</span>
361 &gt; <span class=code>exiftool -xmp-iptcext:all -S a.xmp</span>
362 ArtworkTitle: a title
363
364 <span class=lt># -- Read back as a structure</span>
365 &gt; <span class=code>exiftool -xmp-iptcext:all -S -struct a.xmp</span>
366 ArtworkOrObject: [{AOTitle=a title}]
367
368 <span class=lt># 2. Write another field to the structure as a flattened tag</span>
369 &gt; <span class=code>exiftool -artworkcreator=phil a.xmp</span>
370 1 image files updated
371
372 <span class=lt># -- Note that the structure now has a new field</span>
373 &gt; <span class=code>exiftool -xmp-iptcext:all -S -struct a.xmp</span>
374 ArtworkOrObject: [{AOCreator=[phil],AOTitle=a title}]
375
376 <span class=lt># 3. Add another creator using the "+=" operator</span>
377 &gt; <span class=code>exiftool -artworkcreator+=joe a.xmp</span>
378 1 image files updated
379
380 <span class=lt># -- It was added to the first AOCreator list</span>
381 &gt; <span class=code>exiftool -xmp-iptcext:all -S -struct a.xmp</span>
382 ArtworkOrObject: [{AOCreator=[phil,joe],AOTitle=a title}]
383
384 <span class=lt># 4. Add another artwork title</span>
385 &gt; <span class=code>exiftool -artworktitle+="another one" a.xmp</span>
386 1 image files updated
387
388 <span class=lt># -- This created a new ArtworkOrObject structure in the list of structures
389 # (AOTitle itself is not a list, so a new structure must be created)</span>
390 &gt; <span class=code>exiftool -xmp-iptcext:all -S -struct a.xmp</span>
391 ArtworkOrObject: [{AOCreator=[phil,joe],AOTitle=a title},{AOTitle=another one}]
392
393 <span class=lt># 5. Simply write a different title (do not add with "+=")</span>
394 &gt; <span class=code>exiftool -artworktitle="different" a.xmp</span>
395 1 image files updated
396
397 <span class=lt># -- This deleted all existing AOTitle fields and wrote back only one
398 # (if the second ArtworkOrObject structure had contained more fields, they would have been
399 # preserved, and the second structure would still exist, but without an AOTitle field)</span>
400 &gt; <span class=code>exiftool -xmp-iptcext:all -S -struct a.xmp</span>
401 ArtworkOrObject: [{AOCreator=[phil,joe],AOTitle=different}]
402
403 <span class=lt># 6. Add a completely new structure to the list
404 # (this is very difficult to do properly using flattened tags)</span>
405 &gt; <span class=code>exiftool -artworkorobject+="{aotitle=help,aocreator=[paul,ringo]}" a.xmp</span>
406 1 image files updated
407
408 <span class=lt># -- The new structure was added with the specified fields</span>
409 &gt; <span class=code>exiftool -xmp-iptcext:all -S -struct a.xmp</span>
410 ArtworkOrObject: [{AOCreator=[phil,joe],AOTitle=different},{AOCreator=[paul,ringo],AOTitle=help}]
411
412 <span class=lt># -- See how the relationships are lost when reading as flattened tags</span>
413 &gt; <span class=code>exiftool -xmp-iptcext:all -S a.xmp</span>
414 ArtworkCreator: phil, joe, paul, ringo
415 ArtworkTitle: different, help
416
417 <span class=lt># 7. Delete all structures containing a specific field value</span>
418 &gt; <span class=code>exiftool -artworkorobject-="{AOCreator=phil}" a.xmp</span>
419 1 image files updated
420
421 <span class=lt># -- The ArtworkOrObject list now contains only one structure</span>
422 &gt; <span class=code>exiftool -xmp-iptcext:all -S -struct a.xmp</span>
423 ArtworkOrObject: [{AOCreator=[paul,ringo],AOTitle=help}]</pre></td></tr></table></blockquote>
424
425 <a name='User'></a>
426 <h2>User-Defined Structures</h2>
427
428 <p>User-defined XMP structure tags may be created via the ExifTool config file.
429 See the NewXMPxxxStruct tag definition in the <a href="config.html#xmp-xxx">XMP-xxx
430 examples of the sample config file</a> for more details.</p>
431
432 <hr>
433 <i>Last revised Jun 12, 2023</i>
434 <p class='lf'><a href="index.html">&lt;-- Back to ExifTool home page</a></p>
435 </body>
436 </html>