Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 778
tayl0rs Avatar asked Feb 04 '13 22:02

tayl0rs


2 Answers

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.

like image 140
fmw42 Avatar answered Oct 04 '22 03:10

fmw42


You may pass -set colorspace:auto-grayscale off to convert to disable automatic conversion of RGB channels to a single grayscale channel.

This solution was not yet available at the time of your question, but was introduced in 2015 with version 6.9.2:

2015-07-25 6.9.2-0 Dirk Lemstra <[email protected]>
Added -set colorspace:auto-grayscale=false that will prevent automatic conversion to grayscale inside coders that support grayscale.

like image 35
Stacker Avatar answered Oct 04 '22 01:10

Stacker