LeoHsiao1 / pyexiv2

Read and write image metadata, including EXIF, IPTC, XMP, ICC Profile.
GNU General Public License v3.0
196 stars 39 forks source link

Adding tag description access to pyexiv2 #140

Open ferdnyc opened 1 month ago

ferdnyc commented 1 month ago

A long time ago, in #10, @LeoHsiao1 expressed interest in adding access to tag description fields to pyexiv2, but had trouble accessing the relevant libexiv2 API.

Comment posted by @LeoHsiao1

Hi! I found that pyexiv2 can't edit the two tags you said:

>>> img.modify_exif({'Exif.Photo.MakerNote': 'test', 'Exif.Canon.ShotInfo': 'test'})  
>>> img.read_exif()['Exif.Photo.MakerNote'] 
''
>>> img.read_exif()['Exif.Canon.ShotInfo']  
''

I'm calling exiv2's API just like the example, but I don't know how to read tag description. I tried this Code:

    Exiv2::ExifData::const_iterator end = exifData.end();
    for (Exiv2::ExifData::const_iterator i = exifData.begin(); i != end; ++i) {
        const char* tn = i->tagdesc();

But there was an error in compilation: 'tagdesc': not a member of 'exiv2:: exifdatum'

If you find that API, I can add it to pyexiv2.

Originally posted by @LeoHsiao1 in https://github.com/LeoHsiao1/pyexiv2/issues/10#issuecomment-586200536

Update

@LeoHsiao1: The reason that call didn't work is that the method for accessing the description text is tagDesc(), not tagdesc(), and it actually returns a std::string. So, this should work fine:

    Exiv2::ExifData::const_iterator end = exifData.end();
    for (Exiv2::ExifData::const_iterator i = exifData.begin(); i != end; ++i) {
        std::string description(i->tagDesc());

Or even better, in the world of C++11:

    Exiv2::Image::UniquePtr image = Exiv2::ImageFactory::open(file);
    image->readMetadata();
    Exiv2::ExifData &exifData = image->exifData();

    for (const auto& datum : exifData) {
        std::string description(datum.tagDesc());
        // ...
    }
LeoHsiao1 commented 1 day ago

I'm very sorry, I didn't notice your issue in my inbox until now. Thanks for the sample code! I'll release a new version in the next few days.