如何让 ImageMagick 在复合操作中遵守颜色配置文件?

How can I make ImageMagick respect colour profiles in a composite operation?

我有一个使用 ImageMagick 将两个图像合成在一起的脚本。它在 ImageMagick v6 上运行良好,但在 v7 上输出损坏的图像(准确地说是 macOS 上的 ImageMagick 7.0.8-68 Q16 x86_64 2020-01-14)。

两个输入图像都使用 CMYK 颜色配置文件,它们都已嵌入,但我也有一个单独的文件。我希望输出文件使用相同的颜色配置文件。使用 ImageMagick v7 将图像合成在一起会导致图像损坏,其中颜色 space 更改为 RGB,CMY 通道映射到 RGB 并丢弃 K 通道(因此颜色看起来非常明显错误!)。对这些文件执行其他操作,例如裁剪,输出的结果会按预期保留颜色配置文件和 CMYK 颜色 space;据我所知,只是 composite 的行为发生了变化。

我最初使用的是以下命令:

convert -profile USWebCoatedSWOP.icc under.jpg -profile USWebCoatedSWOP.icc over.jpg -gravity center -composite -profile USWebCoatedSWOP.icc comp.jpg

这在 v6 下有效,但在 v7 下损坏。由于图像嵌入了颜色配置文件,我想知道是否发生了某种颜色 space 转换,因为我也在命令中提供了配置文件文件,所以我尝试了一些其他变体:

# No external profiles specified:
convert under.jpg over.jpg -gravity center -composite comp.jpg
# Embedded input profiles implicit, output profile specified:
convert under.jpg over.jpg -gravity center -composite -profile USWebCoatedSWOP.icc comp.jpg
# Output profile specified twice, as a from–to conversion:
convert under.jpg over.jpg -gravity center -composite -profile USWebCoatedSWOP.icc -profile USWebCoatedSWOP.icc comp.jpg

然而,所有这些都会导致相同的输出。 (它们在 v6 下也会导致一致的输出,除非在那种情况下,输出总是正确的!)添加 -colorspace CMYK 除了使损坏的图像稍微褪色外没有任何效果。添加 -set colorspace CMYK 会导致图像损坏。

ImageMagick 在 v6 和 v7 之间的复合操作中处理颜色配置文件的方式显然发生了一些变化 – 但是什么? changelogs.

来自 identify -verbose 的相关部分。输入文件有:

Image: under.jpg
  Format: JPEG (Joint Photographic Experts Group JFIF format)
  Mime type: image/jpeg
  Class: DirectClass
  Geometry: 1630x2220+0+0
  Resolution: 600x600
  Print size: 2.71667x3.7
  Units: Undefined
  Colorspace: CMYK
  Type: ColorSeparation
…
  Properties:
    date:create: 2020-08-29T13:13:02+00:00
    date:modify: 2020-08-29T12:39:46+00:00
    icc:copyright: Copyright 2000 Adobe Systems, Inc.
    icc:description: U.S. Web Coated (SWOP) v2
    jpeg:colorspace: 4
    jpeg:sampling-factor: 1x1,1x1,1x1,1x1
    signature: d8e1a18efe471b7a131e661ea31e0b02aed6cb9e6555254781fc9df5352f2d6c
  Profiles:
    Profile-icc: 557168 bytes
…

v7 的损坏输出文件有:

Image: comp.jpg
  Format: JPEG (Joint Photographic Experts Group JFIF format)
  Mime type: image/jpeg
  Class: DirectClass
  Geometry: 1630x2220+0+0
  Resolution: 600x600
  Print size: 2.71667x3.7
  Units: Undefined
  Colorspace: sRGB
  Type: TrueColor
…
  Properties:
    date:create: 2020-08-29T13:35:01+00:00
    date:modify: 2020-08-29T13:35:01+00:00
    icc:copyright: Copyright 2000 Adobe Systems, Inc.
    icc:description: U.S. Web Coated (SWOP) v2
    jpeg:colorspace: 2
    jpeg:sampling-factor: 1x1,1x1,1x1
    signature: f1ef274bc6f6f631a406ea3e5c25691faa50ea99ff14ab56ed7b80431f2e64a4
  Profiles:
    Profile-icc: 557168 bytes
…

v6 的正确输出文件有:

Image: comp.jpg
  Format: JPEG (Joint Photographic Experts Group JFIF format)
  Mime type: image/jpeg
  Class: DirectClass
  Geometry: 1630x2220+0+0
  Resolution: 600x600
  Print size: 2.71667x3.7
  Units: Undefined
  Colorspace: CMYK
  Type: ColorSeparation
…  Properties:
    date:create: 2020-08-29T13:25:44+00:00
    date:modify: 2020-08-29T13:24:12+00:00
    icc:copyright: Copyright 2000 Adobe Systems, Inc.
    icc:description: U.S. Web Coated (SWOP) v2
    jpeg:colorspace: 4
    jpeg:sampling-factor: 1x1,1x1,1x1,1x1
    signature: 457347556af05e75fd5cc65f8c1cd1c94dae1ac6fb574c103dc09d8744877928
  Profiles:
    Profile-icc: 557168 bytes
…

损坏输出中 RGB 通道的通道统计数据与正确输出中 CMY 通道的统计数据完全匹配。

编辑: 根据 fmw42 的评论,我还尝试了更严格的 v7 语法:

magick under.jpg -profile USWebCoatedSWOP.icc over.jpg -profile USWebCoatedSWOP.icc -gravity center -composite -profile USWebCoatedSWOP.icc comp.jpg

但是,这仍然会产生损坏的图像。这是嵌入了配置文件的测试图像(比我使用的真实图像小,但仍然存在同样的问题):

这两个命令对我使用 IM 6.9.11.28 和 IM 7.0.10.28 Q16 Mac OSX Sierra 的 ImageMagick 很好用。两者都产生 CMYK 结果并包含 cmyk 配置文件。我怀疑您的 IM 7 版本是旧的并且有一个错误,此后已得到纠正。配置文件从输入到输出。

IM 6

convert under.jpg over.jpg -gravity center -compose over -composite result6.jpg

IM 7

magick under.jpg over.jpg -gravity center -compose over -composite result7.jpg