Page 1 of 1

How to convert PNG to Indexed color (including grayscale images)

Posted: 2019-04-26T14:20:04-07:00
by rchoudhary
I'm trying to convert some small PNG images from 32-bit color mode to indexed color mode.

For color images, I ran the command

Code: Select all

convert IMGS/FLAME.png INDEXED_IMGS/FLAME.png
and it converted fine. For an image that had only grayscale colors, I ran that same command (with the filename changed obviously) but I got a warning:

Code: Select all

convert: profile 'icc': 'RGB ': RGB color space not permitted on grayscale PNG `INDEXED_IMGS/SHADOW.png' @ warning/png.c/MagickPNGWarningHandler/1748.
I ran

Code: Select all

file IMGS/*.png
and got

Code: Select all

IMGS/FLAME.png:  PNG image data, 16 x 16, 8-bit/color RGBA, non-interlaced
IMGS/SHADOW.png: PNG image data, 8 x 8, 8-bit/color RGBA, non-interlaced
which is expected; both images are in 8-bit RGBA mode (since that's the mode I created them in Photoshop). However, when I run

Code: Select all

file INDEXED_IMGS/*.png
I get

Code: Select all

INDEXED_IMGS/FLAME.png:  PNG image data, 16 x 16, 4-bit colormap, non-interlaced
INDEXED_IMGS/SHADOW.png: PNG image data, 8 x 8, 8-bit grayscale, non-interlaced
The 4-bit colormap part checks out, but the grayscale part does not.

So my question is: how can I convert a grayscale image to indexed mode? What really gets me is that it starts out in RGBA mode like the color image, but for some reason it converts automatically to grayscale mode. Is there a way to prevent it from doing that?

I should add that I have a bash script that looks like this:

Code: Select all

#!/bin/bash

for img in IMGS/*.png; do
	file=$(basename $img)
	convert $img INDEXED_IMGS/$file
done
so I don't wanna manually distinguish between grayscale and colored images. If there's a way to do so automatically with some command that's fine though.

Here is info about my ImageMagick tool:

Code: Select all

Version: ImageMagick 7.0.8-42 Q16 x86_64 2019-04-24 https://imagemagick.org
Copyright: © 1999-2019 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenMP 
Delegates (built-in): bzlib freetype heic jng jp2 jpeg lcms ltdl lzma openexr png tiff webp xml zlib

Re: How to convert PNG to Indexed color (including grayscale images)

Posted: 2019-04-26T16:03:09-07:00
by fmw42
For 24-bit color, append the output with PNG8:output

Code: Select all

convert input.png PNG8:output.png
PNG grayscale images do not support color profiles, so you get that warning. But the resulting image should be 8-bit palette.

If you have 32-bit color, then the color under the alpha channel must be a constant color and one not used elsewhere in the image. So you must first set the color under the alpha to some color not used elsewhere in the image. Find such a color after converting to 256 and set the color under the transparency to that color. For example is you have no black in your image after converting to 256 colors, then set the alpha color to black

convert image.png +dither -colors 256 -background black -alpha background PNG8:output.png

Re: How to convert PNG to Indexed color (including grayscale images)

Posted: 2019-04-26T22:54:39-07:00
by snibgo
As Fred says, although as rchoudhary is using v7, I suggest "magick" instead of "convert".

Fred's command includes "+dither", which turns off dithering. An alternative is to leave this out, so the colour-reduction will dither pixels. Dithering is less accurate but can look better (but not noticeably different for ordinary photos and 256 colours).