I'm trying to use
convert(1) from ImageMagick to convert from
.png (and back again later), while preserving exact palette that was used.
Source image is TARGA color indexed file, having a standard 8-bit 256-color palette to 24-bit RGB values. Of those 256 colors, 239 are actually used in image for the pixel values.
However, using simple
convert a.tga a.png, PNG is created which drops unused palette colors, and other palette values get their indexes changed too. Converting to
.gif works just fine and palette is preserved by default (I've verified colormap manually with
identify -verbose a.tiff)
% identify a.png a.tga a.tif a.gif a.png PNG 320x200 320x200+0+0 8-bit sRGB 239c 29549B 0.000u 0:00.000 a.tga TGA 320x200 320x200+0+0 8-bit sRGB 256c 64790B 0.000u 0:00.000 a.tif TIFF 320x200 320x200+0+0 8-bit sRGB 256c 65818B 0.000u 0:00.000 a.gif GIF 320x200 320x200+0+0 8-bit sRGB 256c 29830B 0.000u 0:00.000
What I would like is for palette to remain exactly the same (for example, if in
index 55 had values
R=200 G=150 B=100, the resulting
PNG should have same values
R=200 G=150 B=100 at same index position
55. Also index values not used in that image should also be preserved at same index position with same value (reason being they're going to be used later by 3rd party code which has hardcoded index values for pixel used in overlays)
In essence, I'd like complete color palette to remain exactly the same while converting
Can imagemagick do that and how (or if not, is there an alternative to coding my own tools)?
(I tried playing with few other options, like
png8: etc. but to no avail)
I'm using Debian Buster with
ImageMagick 6.9.10-23 Q16 if it matters.
Update: It seems there exist an option -define png:preserve-colormap=true which sound just like the thing I want, but it does not seem to work when using palette ?!
% convert a.tga -define png:preserve-colormap=true png8:a.png convert-im6.q16: Valid palette required for paletted images `a.png' @ error/png.c/MagickPNGErrorHandler/1641.
(it works without
png8:, but then it creates Truecolor PNG (
color_type:2; without palette) instead of Indexed (
color_type: 3; with palette), which defeats the purpose...