How to convert PNG to Indexed color (including grayscale images)
Posted: 2019-04-26T14:20:04-07:00
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
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:
I ran
and got
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
I get
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:
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:
For color images, I ran the command
Code: Select all
convert IMGS/FLAME.png INDEXED_IMGS/FLAME.png
Code: Select all
convert: profile 'icc': 'RGB ': RGB color space not permitted on grayscale PNG `INDEXED_IMGS/SHADOW.png' @ warning/png.c/MagickPNGWarningHandler/1748.
Code: Select all
file IMGS/*.png
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
Code: Select all
file INDEXED_IMGS/*.png
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
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
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