diff options
-rw-r--r-- | src/libpyexiv2.cpp | 115 | ||||
-rw-r--r-- | src/libpyexiv2.hpp | 19 | ||||
-rw-r--r-- | src/libpyexiv2_wrapper.cpp | 4 | ||||
-rw-r--r-- | src/todo | 3 |
4 files changed, 131 insertions, 10 deletions
diff --git a/src/libpyexiv2.cpp b/src/libpyexiv2.cpp index 9195986..2d09006 100644 --- a/src/libpyexiv2.cpp +++ b/src/libpyexiv2.cpp @@ -22,6 +22,7 @@ File: libpyexiv2.cpp Author(s): Olivier Tilloy <olivier@tilloy.net> History: 28-Dec-06, Olivier Tilloy: created + 30-Dec-06, Olivier Tilloy: implemented IPTC-related methods */ // ***************************************************************************** @@ -118,7 +119,6 @@ namespace LibPyExiv2 boost::python::tuple Image::getExifTag(std::string key) { - boost::python::tuple returnValue; if(_dataRead) { try @@ -128,8 +128,7 @@ namespace LibPyExiv2 if(i != _exifData.end()) { Exiv2::Exifdatum exifDatum = _exifData[key]; - returnValue = boost::python::make_tuple(exifDatum.typeName(), exifDatum.toString()); - return returnValue; + return boost::python::make_tuple(exifDatum.typeName(), exifDatum.toString()); } else { @@ -287,4 +286,114 @@ namespace LibPyExiv2 } } + boost::python::tuple Image::getIptcTag(std::string key) + { + if(_dataRead) + { + try + { + Exiv2::IptcKey iptcKey = Exiv2::IptcKey(key); + Exiv2::IptcMetadata::iterator i = _iptcData.findKey(iptcKey); + if(i != _iptcData.end()) + { + Exiv2::Iptcdatum iptcDatum = _iptcData[key]; + return boost::python::make_tuple(iptcDatum.typeName(), iptcDatum.toString()); + } + else + { + // The key was not found + std::cerr << ">>> Image::getIptcTag(): tag '" << key << "' not found" << std::endl; + return boost::python::make_tuple(std::string(""), std::string("")); + } + } + catch(Exiv2::Error & e) + { + // The key is not a valid Iptc tag key + std::cerr << ">>> Image::getIptcTag(): unknown key '" << key << "'" << std::endl; + return boost::python::make_tuple(std::string(""), std::string("")); + } + } + else + { + std::cerr << ">>> Image::getIptcTag(): metadata not read yet, call Image::readMetadata() first" << std::endl; + return boost::python::make_tuple(std::string(""), std::string("")); + } + } + + boost::python::tuple Image::setIptcTag(std::string key, std::string value) + { + boost::python::tuple returnValue; + if(_dataRead) + { + try + { + Exiv2::IptcKey iptcKey = Exiv2::IptcKey(key); + Exiv2::IptcMetadata::iterator i = _iptcData.findKey(iptcKey); + if(i != _iptcData.end()) + { + Exiv2::Iptcdatum iptcDatum = _iptcData[key]; + returnValue = boost::python::make_tuple(iptcDatum.typeName(), iptcDatum.toString()); + // First erase the existing tag + _iptcData.erase(i); + } + else + { + // The key was not found + std::cerr << ">>> Image::setIptcTag(): tag '" << key << "' not found" << std::endl; + returnValue = boost::python::make_tuple(std::string(""), std::string("")); + } + _iptcData[key] = value; + return returnValue; + } + catch(Exiv2::Error & e) + { + // The key is not a valid Iptc tag key + std::cerr << ">>> Image::setIptcTag(): unknown key '" << key << "'" << std::endl; + return boost::python::make_tuple(std::string(""), std::string("")); + } + } + else + { + std::cerr << ">>> Image::setIptcTag(): metadata not read yet, call Image::readMetadata() first" << std::endl; + return boost::python::make_tuple(std::string(""), std::string("")); + } + } + + boost::python::tuple Image::deleteIptcTag(std::string key) + { + boost::python::tuple returnValue; + if(_dataRead) + { + try + { + Exiv2::IptcKey iptcKey = Exiv2::IptcKey(key); + Exiv2::IptcMetadata::iterator i = _iptcData.findKey(iptcKey); + if(i != _iptcData.end()) + { + Exiv2::Iptcdatum iptcDatum = _iptcData[key]; + returnValue = boost::python::make_tuple(iptcDatum.typeName(), iptcDatum.toString()); + _iptcData.erase(i); + } + else + { + // The key was not found + std::cerr << ">>> Image::deleteIptcTag(): tag '" << key << "' not found" << std::endl; + returnValue = boost::python::make_tuple(std::string(""), std::string("")); + } + return returnValue; + } + catch(Exiv2::Error & e) + { + // The key is not a valid Iptc tag key + std::cerr << ">>> Image::deleteIptcTag(): unknown key '" << key << "'" << std::endl; + return boost::python::make_tuple(std::string(""), std::string("")); + } + } + else + { + std::cerr << ">>> Image::deleteIptcTag(): metadata not read yet, call Image::readMetadata() first" << std::endl; + return boost::python::make_tuple(std::string(""), std::string("")); + } + } + } // End of namespace LibPyExiv2 diff --git a/src/libpyexiv2.hpp b/src/libpyexiv2.hpp index 94a7c33..72bc1a2 100644 --- a/src/libpyexiv2.hpp +++ b/src/libpyexiv2.hpp @@ -22,6 +22,7 @@ File: libpyexiv2.hpp Author(s): Olivier Tilloy <olivier@tilloy.net> History: 28-Dec-06, Olivier Tilloy: created + 30-Dec-06, Olivier Tilloy: added IPTC-related methods */ // ***************************************************************************** @@ -83,11 +84,19 @@ namespace LibPyExiv2 // image. boost::python::list getAvailableIptcTags(); - // TODO - //boost::python::tuple getIptcTag(std::string key); - //std::string getIptcTagToString(std::string key); - //boost::python::tuple setIptcTag(std::string key, std::string value); - //boost::python::tuple deleteIptcTag(std::string key); + // Returns a tuple containing the type (as a string) and the value + // (as a string as well) of the required IPTC tag, empty strings if + // the tag does not exist. + boost::python::tuple getIptcTag(std::string key); + + // Sets the IPTC tag's value and returns a tuple containing the + // type and previous value of the tag. If the tag was not set + // before, it is created. + boost::python::tuple setIptcTag(std::string key, std::string value); + + // Deletes the required IPTC tag and returns a tuple containing the + // type and previous value if it existed, empty strings otherwise. + boost::python::tuple deleteIptcTag(std::string key); private: std::string _filename; diff --git a/src/libpyexiv2_wrapper.cpp b/src/libpyexiv2_wrapper.cpp index 494033e..a8edea2 100644 --- a/src/libpyexiv2_wrapper.cpp +++ b/src/libpyexiv2_wrapper.cpp @@ -22,6 +22,7 @@ File: libpyexiv2_wrapper.cpp Author(s): Olivier Tilloy <olivier@tilloy.net> History: 28-Dec-06, Olivier Tilloy: created + 30-Dec-06, Olivier Tilloy: added IPTC-related methods */ // ***************************************************************************** @@ -44,5 +45,8 @@ BOOST_PYTHON_MODULE(libpyexiv2) .def("setExifTag", &Image::setExifTag) .def("deleteExifTag", &Image::deleteExifTag) .def("getAvailableIptcTags", &Image::getAvailableIptcTags) + .def("getIptcTag", &Image::getIptcTag) + .def("setIptcTag", &Image::setIptcTag) + .def("deleteIptcTag", &Image::deleteIptcTag) ; } @@ -1,6 +1,5 @@ todo list -- implement the IPTC tags methods - Add support for thumbnail extraction -- Add unit extensive tests +- Add extensive unit tests - Document the binding |