22

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.

Community
  • 1
  • 1
tayl0rs
  • 561
  • 2
  • 5
  • 14
  • I would check out the Imagemagick forum; there is a post here that may be of help. http://www.imagemagick.org/discourse-server/viewtopic.php?f=3&t=22654&p=94555&hilit=sRGB+png#p94555 I do not do anything special with png images but from what I see there are lots of different types of png. – Bonzo Feb 05 '13 at 21:35

2 Answers2

13

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.

rubo77
  • 19,527
  • 31
  • 134
  • 226
fmw42
  • 46,825
  • 10
  • 62
  • 80
4

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 <dirk@lem.....org>
Added -set colorspace:auto-grayscale=false that will prevent automatic conversion to grayscale inside coders that support grayscale.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Stacker
  • 1,080
  • 14
  • 20
  • 2
    This is wrong syntax. It is either `-define colorspace:auto-grayscale=false` or `-set colorspace:auto-grayscale false` along with -type tricolor. But these do not work for PNG. The correct way for PNG is to use PNG24:output.png – fmw42 Mar 17 '19 at 21:59
  • Ahhh ffs... I was working with TIFF files and didn't double check if that would work for PNGs as well. Imagemagick and it's inconsistencies... The syntax works flawlessly with TIFFs though – Stacker Mar 17 '19 at 22:19
  • Correct. That syntax works fine for most other formats (e.g. JPG, TIFF, etc). PNG is kind of an exception. – fmw42 Mar 17 '19 at 22:26
  • In my comment above about the syntax, it should have said `-type truecolor` and not `-type tricolor`. My spell checker keeps trying to change what I type and I had not noticed in time. Stacker was correct for other formats in using -define colorspace:auto-grayscale=false. Dirk Lemstra mistyped the syntax. See https://imagemagick.org/script/command-line-options.php#define – fmw42 Mar 17 '19 at 22:40
  • 1
    Wow, this answer was hard to find. I couldn't find any mention of this syntax in the doc. Can anyone elaborate on how was I supposed to know I needed to add a prefix to the output file name? – TalL Jul 23 '20 at 11:43