Cal-linux
Cal-linux

Reputation: 155

Read metadata from a PNG file with imagemagick++

I want to do this (as subject indicates) using the imagemagick++ API. I specifically want to read the focal length used by the (DSLR camera) to shoot. [EDIT] This is part of a program that manipulates the images [END EDIT]

I know I can do it through a system call, invoking the command identify -verbose <image_filename> (e.g., with popen so that I can read the output and look for the line containing FocalLengthIn35mmFilm). But this is gross for two reasons:

Notice that, as the subject says, I am working with PNG files; however, a solution that could work with any lossless image format would be preferred.

Upvotes: 0

Views: 2446

Answers (1)

emcconville
emcconville

Reputation: 24439

ImageMagick is not a EXIF editor, so the best option is to dump the profile EXIF payload into libexif API. But if you are just peaking at a value, you can use ImageMagick's percent escape mechanics to retrieve a value.

For example:

#include <iostream>
#include <string>
#include <Magick++.h>

using namespace Magick;

int main()
{
    InitializeMagick((const char *)nullptr);
    Image img("DSCF2763.JPG");
    Blob data;
    MagickCore::SetImageOption(img.imageInfo(), "format", "%[EXIF:FocalLengthIn35mmFilm]");
    img.write(&data, "INFO");
    std::string value((const char *)data.data(), data.length());
    std::cout << value << std::endl;
}

The above is the same as the CLI command:

convert DSCF2763.JPG -define "format=%[EXIF:FocalLengthIn35mmFilm]" info:

Update

An even easier way would be to use Magick::Image.attribute()

#include <iostream>
#include <string>
#include <Magick++.h>

using namespace Magick;

int main()
{
    InitializeMagick((const char *)nullptr);
    Image img("DSCF2763.JPG");
    std::string value = img.attribute("EXIF:FocalLengthIn35mmFilm");
    std::cout << value << std::endl;
}

Upvotes: 2

Related Questions