防止ImageMagick转换为灰度图像

5

我在Windows上使用ImageMagick-6.9.1来缩放PNG文件,然后再用另一个工具处理它。源文件是RGBA格式,但图像数据是灰度的(所有像素的R、G和B都具有相同的值)。看起来ImageMagick正在检测到这一点,并将输出转换为灰度模式,即使我指定了格式和颜色空间也是如此:

convert.exe src.png -format PNG32 -colorspace sRGB -background transparent -resize 830x540 out.png

我已经验证了输出格式是8位灰度,并在几个应用程序中进行了检查,包括Photoshop。这是一个问题,因为后续的处理步骤无法处理灰度格式,并且会崩溃。将源添加色调可以成功完成此过程,但我们需要能够处理未添加色调的原始文件。
如何强制ImageMagick输出RGB格式的图像,即使它检测到源是灰度的?
更长的日志:(请注意,从颜色类型6到颜色类型4的IHDR转换)
>.\ImageMagick-6.9.1-2\identify.exe -verbose rgba_src.png 
Image: rgba_src.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: DirectClass
  Geometry: 884x747+0+0
  Resolution: 28.35x28.35
  Print size: 31.1817x26.3492
  Units: PixelsPerCentimeter
  Type: GrayscaleAlpha
  Base type: GrayscaleAlpha
  Endianess: Undefined
  Colorspace: Gray
  Depth: 8-bit
  Channel depth:
    gray: 8-bit
    alpha: 8-bit
  Channel statistics:
    Pixels: 660348
    Gray:
      min: 0 (0)
      max: 255 (1)
      mean: 234.605 (0.920019)
      standard deviation: 56.1418 (0.220164)
      kurtosis: 11.312
      skewness: -3.45486
      entropy: 0.271262
    Alpha:
      min: 0 (0)
      max: 247 (0.968627)
      mean: 23.5875 (0.0924999)
      standard deviation: 53.3361 (0.209161)
      kurtosis: 2.71621
      skewness: -2.02496
      entropy: 0.206267
  Alpha: graya(255,0)   #FFFFFF00
  Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.63999,0.33001)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.05999)
    white point: (0.31269,0.32899)
  Background color: graya(255,1)
  Border color: graya(223,1)
  Matte color: graya(189,1)
  Transparent color: graya(0,0)
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 884x747+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: Zip
  Orientation: Undefined
  Properties:
    date:create: 2015-04-21T11:15:21-07:00
    date:modify: 2015-04-21T11:15:28-07:00
    png:cHRM: chunk was found (see Chromaticity, above)
    png:IHDR.bit-depth-orig: 8
    png:IHDR.bit_depth: 8
    png:IHDR.color-type-orig: 6
    png:IHDR.color_type: 6 (RGBA)
    png:IHDR.interlace_method: 0 (Not interlaced)
    png:IHDR.width,height: 884, 747
    png:pHYs: x_res=2835, y_res=2835, units=1
    png:sRGB: intent=0 (Perceptual Intent)
    signature: b30c82674d8ed7b50afbfd0eb86d37346164378ae92ef305b0ab61325e741f52
  Artifacts:
    filename: rgba_src.png
    verbose: true
  Tainted: False
  Filesize: 207KB
  Number pixels: 660K
  Pixels per second: 34.76MB
  User time: 0.016u
  Elapsed time: 0:01.018
  Version: ImageMagick 6.9.1-2 Q16 x86 2015-04-14 http://www.imagemagick.org

>.\ImageMagick-6.9.1-2\convert.exe rgba_src.png -verbose -format PNG32 -colorspace srgb -background transparent -resize 830x540 out.png 
rgba_src.png=>out.png PNG 884x747=>639x540 639x540+0+0 8-bit sRGB 62.8KB 0.406u0:00.107

>.\ImageMagick-6.9.1-2\identify.exe -verbose out.png 
Image: out.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: DirectClass
  Geometry: 639x540+0+0
  Resolution: 28.35x28.35
  Print size: 22.5397x19.0476
  Units: PixelsPerCentimeter
  Type: GrayscaleAlpha
  Base type: Grayscale
  Endianess: Undefined
  Colorspace: Gray
  Depth: 8-bit
  Channel depth:
    gray: 8-bit
    alpha: 8-bit
  Channel statistics:
    Pixels: 345060
    Gray:
      min: 0 (0)
      max: 255 (1)
      mean: 34.1198 (0.133803)
      standard deviation: 77.4794 (0.303841)
      kurtosis: 1.68768
      skewness: 1.88484
      entropy: 0.220331
    Alpha:
      min: 0 (0)
      max: 237 (0.929412)
      mean: 23.5888 (0.092505)
      standard deviation: 53.2132 (0.208679)
      kurtosis: 2.60073
      skewness: -2.00769
      entropy: 0.208417
  Alpha: graya(0,0)   #00000000
  Rendering intent: Undefined
  Gamma: 0.45455
  Chromaticity:
    red primary: (0.63999,0.33001)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.05999)
    white point: (0.31269,0.32899)
  Background color: graya(0,1)
  Border color: graya(223,1)
  Matte color: graya(189,1)
  Transparent color: graya(0,0)
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 639x540+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: Zip
  Orientation: Undefined
  Properties:
    date:create: 2015-04-21T11:41:45-07:00
    date:modify: 2015-04-21T12:24:05-07:00
    png:bKGD: chunk was found (see Background color, above)
    png:cHRM: chunk was found (see Chromaticity, above)
    png:gAMA: gamma=0.45455 (See Gamma, above)
    png:IHDR.bit-depth-orig: 8
    png:IHDR.bit_depth: 8
    png:IHDR.color-type-orig: 4
    png:IHDR.color_type: 4 (GrayAlpha)
    png:IHDR.interlace_method: 0 (Not interlaced)
    png:IHDR.width,height: 639, 540
    png:pHYs: x_res=2835, y_res=2835, units=1
    png:text: 2 tEXt/zTXt/iTXt chunks were found
    png:tIME: 2015-04-21T11:15:28Z
    signature: 3d4b5050f182dac93631d9119d76324d43f9a659ea035523a8e359b7434bdb5b
  Artifacts:
    filename: out.png
    verbose: true
  Tainted: False
  Filesize: 62.8KB
  Number pixels: 345K
  Pixels per second: 57.51MB
  User time: 0.000u
  Elapsed time: 0:01.006
  Version: ImageMagick 6.9.1-2 Q16 x86 2015-04-14 http://www.imagemagick.org

2
你能提供一张输入图像来测试一些想法吗? - Mark Setchell
2
你尝试在命令中添加“-define png:color-type=6”了吗? - Mark Setchell
或者尝试将输出文件指定为 PNG32:out.png - Mark Setchell
"-define png:color-type=6" 看起来已经生效了。如果你想把它放在答案里,我会接受的。 - superstator
太好了!很高兴它对你有用。 - Mark Setchell
在当前版本的ImageMagick(6.9.1)中,“-format IMAGETYPE”选项仅适用于“mogrify”。在其他实用程序中,包括“convert”,-format选项具有不同的含义。 - Glenn Randers-Pehrson
1个回答

12

你可以尝试使用-define来强制输出图像类型为PNG Color-Type 6,例如:

convert input.png ... -define png:color-type=6 output.png

@superstator: 你不仅应该“接受”Mark的答案,还应该给他点个赞… 它完美地符合你的要求。 - Kurt Pfeifle
如果您不关心 alpha 通道,请使用 png:color-type=2。 - scamianbas
我花了很长时间才弄清楚每个人都从哪里获取颜色类型信息。我想这可能与Image Magick在一个开放式的define语句中,更接近于独特的编码器有关。所以它是PNG文件格式的IHDR块的颜色类型枚举整数:https://www.w3.org/TR/PNG/#11IHDR。http://www.libpng.org/pub/png/没有提供帮助。像bibliotecacpa.org.ar这样的Image Magick文档,但可能也包括主要网站,比我可以调用的任何命令都有更多的define命令行选项信息,这也使得它更加困难。 - Pysis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接