EXIF data lost on save

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
superstator
Posts: 17
Joined: 2017-12-08T10:44:16-07:00
Authentication code: 1152

EXIF data lost on save

Post by superstator »

When saving a TIFF, it seems EXIF data is being scrubbed. Is there a way to prevent this? I've come across some discussions about this with the command line tools that suggested using the "-define exif-properties=true" option, but haven't had any luck with it or the Magick.NET equivalent. It is reading the EXIF data just fine from a source TIFF, and I can query EXIF values via MagickImage.GetAttribute(), but after saving to a new file those tags are gone.

Magick.NET:

Code: Select all

        public void Save(string fileName)
        {
            _ImagePixels.SetPixels(_ImageBytes);
            var defines = new WriteDefines { Format = _Image.Format };
            defines.Defines.Add(new Define
            {
                Format = _Image.Format,
                Name = "exif-properties",
                Value = "true"
            });
            _Image.Write(fileName, defines);
        }

        class WriteDefines : IWriteDefines
        {
            public MagickFormat Format { get; set; }

            public List<Define> Defines { get; set; } = new List<Define>();

            IEnumerable<IDefine> IDefines.Defines => Defines;
        }

        class Define : IDefine
        {
            public MagickFormat Format { get; set; }

            public string Name { get; set; }

            public string Value { get; set; }
        }
Commandline:

Code: Select all

magick identify -verbose sample.tif
--snip--
  Properties:
    aux:FlashCompensation: 0/1
    aux:ImageNumber: 596
    aux:Lens: AF-S DX VR Zoom-Nikkor 18-105mm f/3.5-5.6G ED
    aux:LensID: 742170638
    aux:SerialNumber: 3182247
    date:create: 2017-12-08T14:30:40-08:00
    date:modify: 2017-12-08T22:27:24-08:00
    dc:format: image/tiff
    exif:ApertureValue: 6.33985
    exif:CompressedBitsPerPixel: 2
    exif:Contrast: 0
    exif:CustomRendered: 0
    exif:DateTimeDigitized: 2008:08:03 08:03:00
    exif:DateTimeOriginal: 2008:08:03 08:03:00
    exif:DigitalZoomRatio: 1
    exif:ExposureBiasValue: 0
    exif:ExposureMode: 0
    exif:ExposureProgram: 0
    exif:ExposureTime: 0.008
    exif:Flash: 24
    exif:FNumber: 9
    exif:FocalLength: 18
    exif:FocalLengthIn35mmFilm: 27
    exif:ISOSpeedRatings: 400
    exif:LightSource: 0
    exif:MaxApertureValue: 3.6
    exif:MeteringMode: 5
 --snip--
magick convert -define tiff:exif-properties=true sample.tif -negate out.tif
magick identify -verbose out.tif
--snip--
 Properties:
   aux:FlashCompensation: 0/1
   aux:ImageNumber: 596
   aux:Lens: AF-S DX VR Zoom-Nikkor 18-105mm f/3.5-5.6G ED
   aux:LensID: 742170638
   aux:SerialNumber: 3182247
   date:create: 2017-12-13T11:21:11-08:00
   date:modify: 2017-12-13T11:53:08-08:00
   dc:format: image/tiff
   photoshop:ColorMode: 9
   photoshop:DateCreated: 2008-08-03T08:03
   photoshop:ICCProfile: Camera RGB Profile
   signature: be79f573c87bee6f7bf40dd3da4e99ebe971eaa79d8ceed9fe4a9df415e6f462
   tiff:alpha: unspecified
   tiff:endian: lsb
   tiff:photometric: CIELAB
   tiff:rows-per-strip: 850
   xmp:CreateDate: 2008-08-03T08:03
   xmp:CreatorTool: Ver.1.00
   xmp:MetadataDate: 2017-12-08T14:27:17-08:00
   xmp:ModifyDate: 2017-12-08T14:27:17-08:00
   xmpMM:DerivedFrom: EC680DD5F5A58BDC048E110F329A7B7B
   xmpMM:DocumentID: adobe:docid:photoshop:9994541d-3738-4c43-bcbf-312250965360
   xmpMM:InstanceID: xmp.iid:0571e015-474f-6649-9778-d10940c2bb3f
   xmpMM:OriginalDocumentID: EC680DD5F5A58BDC048E110F329A7B7B
 Profiles:
--snip--
Any ideas? Or should I post this in a general forum and figure out the commandline first?
superstator
Posts: 17
Joined: 2017-12-08T10:44:16-07:00
Authentication code: 1152

Re: EXIF data lost on save

Post by superstator »

Well, reading viewtopic.php?t=30518 it sounds like people just gave up on writing Exif to TIFFs with ImageMagick. Unfortunately preserving metadata is a hard requirement for us, so I'll have to look at alternatives. Fingers crossed we can just use LibTiff.NET to copy the metadata tags after writing our image data.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: EXIF data lost on save

Post by fmw42 »

You could also use EXIFTOOL to write your meta data.
superstator
Posts: 17
Joined: 2017-12-08T10:44:16-07:00
Authentication code: 1152

Re: EXIF data lost on save

Post by superstator »

We've used exiftool in the past as a workaround, but being Perl based you're stuck with some ugly hacks to make it work reliably in .NET Core.

Just to summarize what I've learned since, this is a problem with libtiff rather than ImageMagick. Libtiff (and it's derivatives) is deeply broken when it comes to private tag handling, and people have been arguing for a decade and a half about whether and how to fix it. Given it still hasn't happened, and workarounds like exiftool exist, it seems unlikely it'll ever be addressed. In the meantime there are a number of .NET native tools for reading Exif data, but none I've found yet for writing it back. At this point we're looking at rolling our own tool to back up and restore the Exif block.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: EXIF data lost on save

Post by snibgo »

Exiftool is open source (see http://owl.phy.queensu.ca/~phil/exiftool/ ). Perhaps you can hack what you need from that.
snibgo's IM pages: im.snibgo.com
superstator
Posts: 17
Joined: 2017-12-08T10:44:16-07:00
Authentication code: 1152

Re: EXIF data lost on save

Post by superstator »

It would be a worthwhile project someday to try and do a faithful port of Exiftool to something more modern (or at least more interop friendly) than Perl 5. Today is probably not the day :) .

I've got a working C# Exif copier now. IM/libtiff is stripping a few other non-Exif related tags as well, so I still need to track those down, but otherwise things are looking up.
246246
Posts: 190
Joined: 2015-07-06T07:38:22-07:00
Authentication code: 1151

Re: EXIF data lost on save

Post by 246246 »

Have you tried exiv2, that are written in C++.
I once tried to use it via c++/CLI, reading from the original and write to newly saved file.
metacopy.cpp would be your help.
Post Reply