ImageMagick: convert keeps changing the colorspace to Gray. How to preserve sRGB colorspace?

tayl0rs picture tayl0rs · Feb 4, 2013 · Viewed 8.2k times · Source

I have a batch script that converts my PNG-24 (with Transparency) images to 50% and 25% size (for mobile development). Usually these images have colors in them but now I am trying to convert an image that has no colors and ImageMagick keeps changing the colorspace profile to "Gray", which messes up my image in the 3d engine I'm using (Unity).

I have tried forcing it to use type TrueColor, colorspace sRGB, and the sRGB.icc profile (the one included with OSX) but it doesn't seem to care. It still changes it to Gray.

> convert old.png -profile srgb.icc -colorspace sRGB -type TrueColor new.png
> identify *.png
  old.png PNG 140x140 140x140+0+0 8-bit sRGB 3.68KB 0.000u 0:00.000
  new.png PNG 140x140 140x140+0+0 8-bit sRGB 256c 2.33KB 0.000u 0:00.000

ImageMagick still identifies it as an 8-bit sRGB image but it puts "256c" after it which I'm assuming means it has reduced it down to 256 colors, which I don't want either. When I look at the image in OSX Preview.app, it says it is using the Gray color profile. The image also visually looks a lot different.

Here is the image I'm using: https://dl.dropbox.com/u/59304/old.png

There is a duplicate question here, ImageMagick Reduces Colorspace to Gray, but the answer does not work for me and I don't have enough reputation to comment on his answer, unfortunately. I imagine my case is different because I'm using PNG and not JPG.

Version: ImageMagick 6.8.0-7 2013-01-02 Q16 http://www.imagemagick.org
Features:  OpenCL 

edit- After reading the ImageMagick forums as specified in one of the answers, it looks like just prepending PNG32: or PNG24: to the output file solves the problem.

Answer

fmw42 picture fmw42 · Mar 17, 2019

The proper way to keep a grayscale PNG as RGB is to use PNG24:result.png

Input:

enter image description here

convert lena.png -colorspace gray PNG24:lenag_rgb.png

identify -verbose lenag_rgb.png

Image: lenag_rgb.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: DirectClass
  Geometry: 256x256+0+0
  Units: Undefined
  Colorspace: sRGB
  Type: Grayscale

So as you see above, Colorspace is RGB while the type is Grayscale.

enter image description here

For other image formats such as JPG and TIFF, use -define colorspace:auto-grayscale=false along with -type truecolor.