Convert 8bit sRGB to 4bit grayscale with ImageMagick


I want to print an 8bit sRGB PNG image on an Epson TM-T88v receipt printer. The printer needs a 4bit grayscale PNG file to print it correctly.

As I'm using PHP with ImageMagick, I'm not sure which commands are needed to convert the image.

If I use ImageMagick's "identify" and "convert" on the linux console on the 8bit input image I get following output:

test.png PNG 128x128 128x128+0+0 8-bit sRGB 1.78KB 0.000u 0:00.000

I've tried...

convert test.png -depth 4 -colorspace gray test-4bit.png

which gives me still an 8bit image instead of 4bit

test-4bit.png PNG 128x128 128x128+0+0 8-bit sRGB 2.58KB 0.000u 0:00.000

Any suggestions?

8/11/2016 12:12:00 PM

Accepted Answer

No idea why but identify seems to always assume sRGB. Your image should actually be grayscale. If you use the -verbose option it will report the correct color space.

For the bit-depth you need to explicitly set the color-type and bit-depth for the PNG encoder by using -define.

This should work:

convert test.png -depth 4 -colorspace gray -define png:color-type=0 -define png:bit-depth=4 test-4bit-gray.png
8/11/2016 1:25:00 PM