The input image is color (not grayscale) but with very few colors (<16 colors). The output image must be 8 bits per pixel (not 4 bit per pixel/not 8bit per color/not grayscale) BMP. The converion must be lossless. (unless the colors more than 256)
What ImageMagick parameters should I use to achieve it ?
Thanks!
I have tried some parameters like:
-colors 256
-type Palette or optimize
-depth 8
But they all output a 4 bits per pixel BMP. I want to ensure the output image a 8 bit per pixel BMP.
Any command-line solution other than ImageMagick is welcomed.
I'd suggest NetPBM and specifically its ppmtobmp
tool. If we use a command of the following type to create a PPM file with any desired number of colours:
magick -size 64x64 xc: +noise random -colors 9 ppm:- | ...
we can then examine what ppmtobmp
does.
So firstly the default, which is to write the smallest possible image given the number of colours present (the same as what ImageMagick would do):
magick -size 64x64 xc: +noise random -colors 9 ppm:- | ppmtobmp > result.bmp
ppmtobmp: analyzing colors...
ppmtobmp: 9 colors found
ppmtobmp: Writing 4 bits per pixel with a color palette
But if we specify -bpp 8
, we get:
magick -size 64x64 xc: +noise random -colors 9 ppm:- | ppmtobmp -bpp 8 > result.bmp
ppmtobmp: analyzing colors...
ppmtobmp: 9 colors found
ppmtobmp: Writing 8 bits per pixel with a color palette
And if we submit too many colours for a palette image, it aborts with a failed status ($?=1):
magick -size 64x64 xc: +noise random -colors 258 ppm:- | ppmtobmp -bpp 8 > result.bmp
ppmtobmp: analyzing colors...
ppmtobmp: More than 256 colors found
ppmtobmp: There are too many colors in the image to represent in the number of bits per pixel you requested: 8. You may use Pnmquant to reduce the number of colors in the image.