← Back to team overview

pyexiv2-developers team mailing list archive

Re: pyexiv2 0.2 testing

 

Hey Damien,

Thanks for your questions. Please find my answers inline, and don't hesitate to ask if you have more doubts/requests!

On 2010-02-17, Damien Moore <damienlmoore@xxxxxxxxx> wrote:
although there is no bug report to track it, could you please file one?

will do.

Thanks!

Now I'm having some minor issues with Xmp. I doubt it matters, but I
was working on this image:
http://farm5.static.flickr.com/4050/4346878811_b0c030a8fa_o.jpg

in python
import pyexiv2
meta=pyexiv2.ImageMetadata("test.jpg")
meta.read()
meta.xmp_keys
[]
key = 'Xmp.dc.title'
value = {'x-default': 'Sunset', 'fr': 'Coucher de soleil'}
meta[key]=pyexiv2.XmpTag(key,value)
meta[key]
meta[key]="Sunset"
<Xmp.dc.title [Lang Alt] = {'x-default': 'snow', 'fr': 'neige'}>
#Seems to work fine, but why can't I just use: meta[key]=value  (i.e.
what can you do in XmpTag constructor that you can't do in
__setitem__?). Also, using a dict is kind of verbose if I just want to
set x-default, no?

meta[key] always returns a {Exif,Iptc,Xmp}Tag object. That's a bit more code to write for application developers, but that's also much more flexible: a tag object contains more information than just a value. Allowing to set the value directly with an idiom such as meta[key]="Sunset" would be possible, but would break consistency, and imply defining possibly non-intuitive default behaviours. E.g., should I assume that the user wants to set the value, or the raw_value?

LangAlt tags always translate to dictionary-like values (because that's the very definition of a LangAlt in the XMP specification). Again, that's more work for the application developer, but provides a consistent API. You can still write some helper functions that either accept a dict or a single value, and set a dict with x-default with this single value.

meta["Xmp.dc.subject"]=["abc","def","ghi"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pyexiv2/metadata.py", line 275, in __setitem__
    return getattr(self, '_set_%s_tag' % family)(tag)
  File "pyexiv2/metadata.py", line 229, in _set_xmp_tag
    raise TypeError('Expecting an XmpTag')
TypeError: Expecting an XmpTag
#this is straight from your examples.py so I'm surprised this doesn't work

Where in the examples.py script did you find that?
Again, the [] operator on the ImageMetadata object only accepts {Exif,Iptc,Xmp}Tag objects. If you want to set the value of an existing tag, you can do: meta[key].value = value If you don't know in advance whether the metadata already contains the tag, the following conditional code is what you want to use:

if key in meta.xmp_keys:
    meta[key].value = value
else:
    meta[key] = XmpTag(key, value)

key="Xmp.dc.subject"
value={'x-default':['snow','home']}
meta[key]=pyexiv2.XmpTag(key,value)
print meta[key]
{'x-default': ['test2', 'test3']}
#works as expected

Actually, it shouldn't. According to the XMP specification, the type of Xmp.dc.subject is "bag Text", meaning it expects a list of values, not a dictionary (it's not a LangAlt).

Some additional questions:
* In the value, what is 'x-default'? (does this write to whatever the
users locale is or just represents the absence of a locale?) Are there
other possibilities here besides language codes?

See the "Languages Alternatives" section in the XMP specification (page 18 of the PDF document). I'm partially quoting it:

  XMP uses the "x-default" language code to denote the default value for
  a language alternative.

  The language qualifier is a property qualifier, [...] and its value is
  a string that conforms to RFC 3066 notation.

* Can all Xmp tags be set with the meta[key]=XmpTag(key,value) idiom?

Yes they can. If you don't want to write the conditional code suggested above, you can still set (or override) the value of a tag using this idiom.

If so is value always a dict or can it be something else? The dict
always refers to the languages? What about non-text types like dates
or numbers, such as the Xmp.exif.DateTimeOriginal (or are they just
stored as text too?)

As stated in the documentation (which by the way I finally completed and committed yesterday), the python type of the value depends on the XMP type of the tag. There is a types translation table in the documentation. For instance, LangAlt translates to dict, Date translates to datetime.date, ...

* Is it ever possible to use meta[key]=value? (I gather from the
example that it should work for Xmp.dc.subject)

No it's not allowed, it will always raise a TypeError.

* Is there a way to discover from within the lib what keys can be set?

Not currently. It may be possible to implement depending on what libexiv2 exposes on this side, though.


I hope this helps.

Olivier



Follow ups

References