使用ImageMagick转换为灰色的1位,alpha 8位

时间:2016-07-11 13:06:11

标签: android imagemagick

我正在尝试将Android应用资源(图标和背景)转换为相同格式的素材图标(为了节省空间和色调):

enter image description here

$ identify -verbose ic_alarm_black_48dp.png 
Image: ic_alarm_black_48dp.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: DirectClass
  Geometry: 192x192+0+0
  Units: Undefined
  Type: Bilevel
  Base type: Bilevel
  Endianess: Undefined
  Colorspace: Gray
  Depth: 8-bit
  Channel depth:
    gray: 1-bit
    alpha: 8-bit

此图像具有8位深度和1位灰色通道深度。

这是我想要转换为上面格式的测试图像(透明背景上有阴影的形状):

icon to be converted

$ identify -verbose a.png 
Image: a.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: DirectClass
  Geometry: 256x256+0+0
  Units: Undefined
  Type: TrueColorAlpha
  Endianess: Undefined
  Colorspace: sRGB
  Depth: 8-bit
  Channel depth:
    red: 8-bit
    green: 8-bit
    blue: 8-bit
    alpha: 8-bit

我希望得到一个8位alpha通道的1位灰度图像。

我觉得我需要将源图像转换为1位灰度图像(所有非透明像素设置为1)并在顶部应用提取的alpha蒙版。

转换为1位灰度

`convert a.png -alpha extract -threshold 0 -negate -transparent white a-trans.png`

在顶部涂抹面膜

`convert a-trans.png \( a.png -alpha extract \) -alpha off -compose copy_opacity -composite a-result.png`

expected result

$ identify -verbose a-result.png 
Image: a-result.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: DirectClass
  Geometry: 256x256+0+0
  Units: Undefined
  Type: Bilevel
  Base type: Bilevel
  Endianess: Undefined
  Colorspace: Gray
  Depth: 8-bit
  Channel depth:
    gray: 1-bit
    alpha: 8-bit

最后的图像是我想要实现的。可以优化转换吗?

1 个答案:

答案 0 :(得分:1)

我认为你的意思是:

convert input.png -channel RGB -fill black -colorize 100%  result.png

enter image description here

Image: result.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: DirectClass
  Geometry: 256x256+0+0
  Units: Undefined
  Type: Bilevel
  Base type: Grayscale
  Endianess: Undefined
  Colorspace: Gray
  Depth: 8-bit
  Channel depth:
    gray: 1-bit
    alpha: 8-bit