I'm trying to use convert(1) from ImageMagick to convert from .tga to .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 .tiff or .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 TGA 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 TGA->PNG->TGA.

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 -colors 256, -remap a.tga, 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...

1 Answer 1


This question is informatively related, and also gives surprisingly simple perl script workaround PNG writer to manually work around the problem if I'd run out of options.

However, I've tried GraphicsMagick 1.3.35 package as alternative, and while it doesn't seem to work with my TGA files (although it claims it does), it does provide part of the solution.

In the end, it was combining ImageMagick and GraphicsMagick that did the trick, producing color-indexed PNG with same palette as in TGA with this pipeline:

convert a.tga tiff:- | gm convert - a.png

It is kludgy, but it works. I'd still love to see cleaner solution if anyone has it...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.