1

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?

Community
  • 1
  • 1
Sam
  • 1,384
  • 2
  • 20
  • 30

1 Answers1

2

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.

dlemstra
  • 7,813
  • 2
  • 27
  • 43
  • I suspected something like this, but thank you for confirming information. After getting info JPEG specifications I concluded that it is definitely better to use specific, well supported tool rather than manual intervention into jpeg with possible unexpected consequences :) Last question: if it technically possible - what are the chances that one day we will see feature like 'exif edit without content loading' in Magick.NET? – Sam Apr 08 '16 at 08:00
  • Well, this feature seems not so simple to implement as I thought, so we'll find some other way to boost exif edit for a while. But we will glad to see this feature in the future :) Anyway, thanks for you answers and for Magick.NET at all! – Sam Apr 08 '16 at 10:04