将标签值添加到图像中现有的 exif 配置文件

Add tag value to existing exif profile in image

我使用 Magick.NET library for reading exif values. For highest performance I'm using Ping method (see this) 因为我在处理一些图像元数据时不需要实际的图像内容。

但是如果这个值不存在,我现在需要写入 exif 标签值。我想做什么:

    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);
            }
        }
    }

但是这段代码不是将标签值写入exif。当以另一种方式打开文件时,我可以成功写入标签值(如 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();

            ...
        }

但是这个解码整个图像内容,我不需要它来改变'content-independent'标签的值,比如版权DateTimeDigitized.

问题是:如何编辑这个exif标签而不加载整个图像内容?
我需要重写整个 exif 配置文件 (image.AddProfile(profile, true)) 还是有一些方法可以只编辑配置文件?

遗憾的是,在 Magick.NET 中无法执行此操作。如果要写入图像,则需要使用 .Read 读取图像的所有像素。 .Ping 方法只适用于在不加载整个图像的情况下从图像中获取信息的情况。

无法 replace/edit 只有一个值。您正在正确替换配置文件。不过您不需要 true,因为这是 AddProfile 的默认值。

可能还有另一个图书馆可以帮助您解决这个问题。您还可以阅读 JPEG 规范并找到 EXIF 配置文件在文件中的位置。然后使用ExifProfile class修改它并写回图像。我将把那部分留作 reader.

的练习

这个功能也不太可能最终出现在 ImageMagick 中,因为这意味着我们将不得不重写我们编码和解码图像的方式。作为 Magick.NET 的维护者,我可以在将来添加这个额外的功能。我已经编写了 ImageOptimizers,可用于减小不属于 ImageMagick 的 JPG/PNG 图像的大小。但我不能告诉你什么时候可用。