I am currently working on a Python script that inverts the colors of a given image file. It supports JPG, PNG and TGA file formats.
To test the TGA format, I downloaded a set of sample files. The two files that concern my question are CCM8.TGA and UCM8.TGA. These are respectively compressed and uncompressed indexed color 8-bit gamma integer images.
I am unfamiliar with all the various elements that take part when constructing an image so please forgive me if I use incorrect terminology or I state or miss the obvious.
What I am trying to do is to invert the color of these 'P' images getting the RGB palette, inverting it and then putting it back. The problem is that some of the color indexes are different to what I expect and what I get when I use ImageMagick to check the file attributes or visualize the files with GIMP, so there must be other elements at play that I am unaware of.
This is how I retrieve and save the pixel mapping and color palette to text files for debugging.
from PIL import Image
import numpy as np
im = Image.open('CCM8.TGA')
d = list(im.getdata())
p = np.array(im.getpalette(),dtype=np.uint8).reshape((256,3))
with open("data.txt", "w") as file:
file.write(str(d))
with open("palette.txt", "w") as file:
file.write(str(p))
Looking at data.txt, I see that pixel (0,0) is indexed to entry 64 of the RGB palette, and pixel (0,8) is indexed to 128. When I look to the corresponding entries in palette.txt, I find:
64: [123 129 0]
128: [ 0 125 0]
But I know that pixel (0,0) is RGB 255,0,0 and pixel (0,8) is 0,255,0 as these are red and green respectively and not a shade of these colors.
I firstly confirmed this using the manual method of opening the image with GIMP and picking the colors. Later, I found out I could use identify from ImageMagick which gave me the values for those indexes:
identify -verbose CCM8.TGA
...
Colormap entries: 256
Colormap:
...
64: (255,0,0) #FF0000 red
...
128: (0,255,0) #00FF00 lime
...
Pixel (0,16) is blue, indexed to 192, and the values I get with PIL and ImageMagick are consistent: 0,0,255. Same as (0,24) which is black, indexed to 0, which is 0,0,0 in both cases. Finally, pixel (0,56) is white and I get 123,255,255 using PIL and 255,255,255 using identify.
There are no more colors on that image.
I have come to the conclusion that there are some other elements that I am missing which determine the 'final' values of the color palette but I really don't know enough about images and my search for an answer has been unfruitful so far.
I would appreciate if someone could point me in the right direction to understand why I am getting different values when I read the palette directly using PIL and what I see using ImageMagick's identify or GIMP.
Thank you.

