Skip to content

Update EXIF metadata without re-encoding JPEG image #530

Open
@Webreaper

Description

@Webreaper

I'm building an image processing app using .Net Core 3.0 and blazor. I'm using Magick.Net (latest version, Q16-AnyCPU, running on OSX and also on Linux) to read from and write to the IPTC tag list. I have two questions:

  1. My code to add a tag looks something like this:

         string tagToAdd = "New Tag";
         // Read image from file
         using (MagickImage image = new MagickImage(input))
         {
             // Retrieve the exif information
             var profile = image.GetIptcProfile();
    
             if( profile == null )
                 profile = new IptcProfile();
    
             var keywords = profile.Values.Where(x => x.Tag == IptcTag.Keyword)
                                   .Select( x => x.Value )
                                   .ToList();
    
             keywords.Add(tagToAdd);
             string allKeywords = string.Join(",", keywords);
             profile.SetValue(IptcTag.Keyword, allKeywords);
             image.AddProfile(profile);
    
             image.Write(output);
         }
    

Is this the right approach? Is there a better way? There aren't really any good code samples for IPTC writing, so I wanted to check I've not missed an API to just add a single keyword to the existing set of keywords.

  1. Assuming the code above is correct, is this guaranteed to be a no-op on the actual image itself? i.e., what I'm looking for is to just rewrite the IPTC Exif block without re-writing the image. What I don't want this to do is re-encode the JPEG (or touch it at all) because obviously if it does that, after adding 10 keywords in separate operations I'll end up with blotchy artifacts in my images. I know that ExifTool does the tags addition/removal without touching the image data, but want to check if the same assumption for ImageMagick/Magick.Net.

The reason this question cropped up is because I had a 7.6MB image on disk, added a single keyword using the code above, and the resulting image was 6.8MB - which seems to me like it may have re-encoded the image and lost some data into the process. I could just spawn a process and run exiftool, but it's cleaner to do the IPTC tag changes in-process, particularly when the thing I'm writing is cross-platform.

Thanks for the help!

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions