Reputation: 1424
I use Magick.NET library for reading exif values. For highest performance I'm using Ping
method (see this) because I don't need actual image content when working with some image metadata.
But now I need to write exif tag value if this value not exists. What I'm trying to do:
public static void Test()
{
var path = @"C:\image.jpg";
using (var image = new MagickImage())
{
image.Ping(path);
var profile = image.GetExifProfile();
var copyright = profile.Values.FirstOrDefault(x => x.Tag == ExifTag.Copyright);
if (copyright != null)
{
Trace.WriteLine("Copyright: " + copyright);
}
else
{
Trace.WriteLine("Write Copyright data");
profile.SetValue(ExifTag.Copyright, "Example text");
image.AddProfile(profile, true);
image.Write(path);
}
}
}
But this code is not writing tag value to exif. I can successfully write tag value when opening file another way (like in this answer):
var path = @"C:\image.jpg";
// Read image with content
using (var image = new MagickImage(path))
{
// image.Ping(path) - no more need
var profile = image.GetExifProfile();
...
}
But this decode whole image content, I don't need it for changing values of 'content-independent' tags like Copyright or DateTimeDigitized.
The question is: how to edit this exif tags without loading whole image content?
And do I need to rewrite whole exif profile (image.AddProfile(profile, true)
) or there is some way to just edit profile?
Upvotes: 1
Views: 1400
Reputation: 8163
Unfortunately there is no way to do this in Magick.NET. You need to read all the pixels of the image with .Read
if you want to write the image. The .Ping
method should only be used if you want to get information from the image without loading the whole image.
There is no way to replace/edit just one value. You are replacing the profile correctly. You don't need the true
though because that is the default value for AddProfile.
There might be another library that could help you with this. You could also read the JPEG specifications and find the position of the EXIF profile in your file. Then use the ExifProfile class to modify it and write it back to the image. I'll leave that part as an exercise for the reader.
It is also not very likely that this feature will end up in ImageMagick because that would mean we would have to rewrite the way we encode and decode images. As the maintainer of Magick.NET I could add this extra feature in the future. I already wrote ImageOptimizers that can be used to reduce the size of a JPG/PNG images which are not part of ImageMagick. But I can't tell you when that will be available.
Upvotes: 2