diff options
author | Olivier Tilloy <olivier@tilloy.net> | 2009-03-24 09:53:17 +0100 |
---|---|---|
committer | Olivier Tilloy <olivier@tilloy.net> | 2009-03-24 09:53:17 +0100 |
commit | 1a28d13266e2993c52ededd3cfda3aa708c4e7c3 (patch) | |
tree | 79acbeb3d401e6f5b1d1c013bf5c505c4dd61173 /src | |
parent | e78316f24a60c79ace82f537e83eed2d0261ab2e (diff) | |
download | pyexiv2-1a28d13266e2993c52ededd3cfda3aa708c4e7c3.tar.gz |
Private setters for EXIF tags.
Diffstat (limited to 'src')
-rw-r--r-- | src/exiv2wrapper.cpp | 21 | ||||
-rw-r--r-- | src/exiv2wrapper.hpp | 7 | ||||
-rw-r--r-- | src/exiv2wrapper_python.cpp | 2 | ||||
-rw-r--r-- | src/pyexiv2.py | 43 |
4 files changed, 45 insertions, 28 deletions
diff --git a/src/exiv2wrapper.cpp b/src/exiv2wrapper.cpp index 8d22e69..83a3c95 100644 --- a/src/exiv2wrapper.cpp +++ b/src/exiv2wrapper.cpp @@ -123,36 +123,27 @@ boost::python::tuple Image::getExifTag(std::string key) } } -/* -boost::python::tuple Image::setExifTag(std::string key, std::string value) +void Image::setExifTag(std::string key, std::string value) { - boost::python::tuple returnValue; if(_dataRead) { Exiv2::ExifKey exifKey = Exiv2::ExifKey(key); Exiv2::ExifMetadata::iterator i = _exifData.findKey(exifKey); if(i != _exifData.end()) { - Exiv2::Exifdatum exifDatum = _exifData[key]; - returnValue = boost::python::make_tuple(std::string(exifDatum.typeName()), exifDatum.toString()); - // First erase the existing tag: in some case (and - // I don't know why), the new value won't replace - // the old one if not previously erased... + // First erase the existing tag: in some case (and I don't know + // why), the new value won't replace the old one if not previously + // erased... + // TODO: check if this is still valid with libexiv2 0.18 _exifData.erase(i); } - else - { - // The key was not found - returnValue = boost::python::make_tuple(std::string(""), std::string("")); - } _exifData[key] = value; - return returnValue; } else throw Exiv2::Error(METADATA_NOT_READ); } -boost::python::tuple Image::deleteExifTag(std::string key) +/*boost::python::tuple Image::deleteExifTag(std::string key) { boost::python::tuple returnValue; if(_dataRead) diff --git a/src/exiv2wrapper.hpp b/src/exiv2wrapper.hpp index 2d33cb7..5eb6cfa 100644 --- a/src/exiv2wrapper.hpp +++ b/src/exiv2wrapper.hpp @@ -67,10 +67,9 @@ public: // tagvalue (human-readable) boost::python::tuple getExifTag(std::string key); - // Set the EXIF tag's value and return a tuple containing the - // type and previous value of the tag (empty strings if not previously - // set). If the tag was not previously set, it is created. - //boost::python::tuple setExifTag(std::string key, std::string value); + // Set the EXIF tag's value. If the tag was not previously set, it is + // created. + void setExifTag(std::string key, std::string value); // Delete the required EXIF tag and return a tuple containing the // type and previous value. diff --git a/src/exiv2wrapper_python.cpp b/src/exiv2wrapper_python.cpp index db97a0d..7ee457c 100644 --- a/src/exiv2wrapper_python.cpp +++ b/src/exiv2wrapper_python.cpp @@ -54,7 +54,7 @@ BOOST_PYTHON_MODULE(libexiv2python) .def("exifKeys", &Image::exifKeys) .def("getExifTag", &Image::getExifTag) -// .def("_Image__setExifTag", &Image::setExifTag) + .def("setExifTag", &Image::setExifTag) // .def("_Image__deleteExifTag", &Image::deleteExifTag) .def("iptcKeys", &Image::iptcKeys) diff --git a/src/pyexiv2.py b/src/pyexiv2.py index 9360b60..5e76a7d 100644 --- a/src/pyexiv2.py +++ b/src/pyexiv2.py @@ -250,9 +250,7 @@ class MetadataTag(object): """ A generic metadata tag. - - TODO: determine which attributes are common to all types of tags (EXIF, - IPTC and XMP), and which are specific. + DOCME """ def __init__(self, key, name, label, description, xtype, value): @@ -265,6 +263,8 @@ class MetadataTag(object): self.description = description self.xtype = xtype self._value = value + # Reference to the containing ImageMetadata object + self.metadata = None def __str__(self): """ @@ -297,10 +297,10 @@ class ExifTag(MetadataTag): """ # According to the EXIF specification, the only accepted format for an Ascii - # value representing a datetime is '%Y-%m-%d %H:%M:%S', but it seems that + # value representing a datetime is '%Y:%m:%d %H:%M:%S', but it seems that # others formats can be found in the wild. - _datetime_formats = ('%Y-%m-%d %H:%M:%S', - '%Y:%m:%d %H:%M:%S', + _datetime_formats = ('%Y:%m:%d %H:%M:%S', + '%Y-%m-%d %H:%M:%S', '%Y-%m-%dT%H:%M:%SZ') def __init__(self, key, name, label, description, xtype, value, fvalue): @@ -399,9 +399,9 @@ class ExifTag(MetadataTag): """ if xtype == 'Ascii': if type(value) is datetime.datetime: - return value.strftime('%Y-%m-%d %H:%M:%S') + return value.strftime('%Y:%m:%d %H:%M:%S') elif type(value) is datetime.date: - return value.strftime('%Y-%m-%d 00:00:00') + return value.strftime('%Y:%m:%d 00:00:00') elif type(value) is unicode: try: return value.encode('utf-8') @@ -466,6 +466,14 @@ class ExifTag(MetadataTag): raise ExifValueError(value, xtype) + def to_string(self): + """ + Return a string representation of the EXIF tag suitable to pass to + libexiv2 to set the value of the tag. + DOCME + """ + return ExifTag._convert_to_string(self.value, self.xtype) + def __str__(self): """ Return a string representation of the EXIF tag. @@ -1003,6 +1011,7 @@ class ImageMetadata(object): return self._tags['exif'][key] except KeyError: tag = ExifTag(*self._image.getExifTag(key)) + tag.metadata = self self._tags['exif'][key] = tag return tag @@ -1011,6 +1020,7 @@ class ImageMetadata(object): return self._tags['iptc'][key] except KeyError: tag = IptcTag(*self._image.getIptcTag(key)) + tag.metadata = self self._tags['iptc'][key] = tag return tag @@ -1019,6 +1029,7 @@ class ImageMetadata(object): return self._tags['xmp'][key] except KeyError: tag = XmpTag(*self._image.getXmpTag(key)) + tag.metadata = self self._tags['xmp'][key] = tag return tag @@ -1033,6 +1044,22 @@ class ImageMetadata(object): except AttributeError: raise KeyError(key) + def _set_exif_tag(self, tag): + if type(tag) is not ExifTag: + raise TypeError('Expecting an ExifTag') + self._image.setExifTag(tag.key, tag.to_string()) + self._tags['exif'][tag.key] = tag + tag.metadata = self + + def _set_exif_tag_value(self, key, value): + # Overwrite the tag value for an already existing tag. + # The tag is already in cache. + if key not in self.exif_keys: + raise KeyError('Cannot set the value of an inexistent tag') + if type(value) is not str: + raise TypeError('Expecting a string') + self._image.setExifTag(key, value) + class Image(libexiv2python.Image): |