programc7r
programc7r

Reputation: 65

Python 3 script or gimp script-fu to rearrange color map Index images

Previously I wanted to swap color values in palette of indexed color map but that seems to be not what im looking to do... as then colors on the image also swap (black becomes magenta and magenta becomes black, it's a mess). I need to arrange colors order in palette, or particularly move Magenta color to be first color of already indexed image.

GIMP has Map - Rearrange Color map, a window allows you to drag your Magenta to be first (Index 0) and then you have it first, no actual color value swap, just order in the color map. But that's not a batch process, thus looking for a script.

But can Python do it with GIMP using the Rearrange color (find Index# containing 255,0,255 that will be different Index # for each opened image) and then move Index# to first image for each opened image, then save/export all png's. Setting Index 0 color to Magenta and Index# to black that is usually Index 0 is not an option, it changes the value of the colors, and thus image colors! Has to be arrange indexes

OR python without gimp. As a stand alone script.

Upvotes: 0

Views: 660

Answers (1)

xenoid
xenoid

Reputation: 8904

In Gimp:

pdb.plug_in_colormap_remap(image, drawable, num_colors, map)

This procedure takes an indexed image and lets you alter 
the positions of colors in the colormap without visually 
changing the image.

map: Remap array for the colormap

So you would just have to compute the map that transform an image palette into your required palette.

In practice, you can get the colors in the map that way:

_, colormap = pdb.gimp_image_get_colormap(image)
colors=[tuple(colormap[i:i+3]) for i in range(0,len(colormap),3)]

yields:

[(180, 113, 205), (189, 142, 104), (130, 176, 204), (156, 195, 140), (107, 218, 136), (130, 218, 72)]

The map list is just a list of indices (the value at position X is the index in the result colormap of the color at position X in the current map). So for instance to swap first and last:

pdb.plug_in_colormap_remap(image, None, 6, [5,1,2,3,4,0])

(the drawable parameter is ignored)

Of course you have to compute the map, but if you already have a reference colormap as a list of triplets this is just:

map=[reference.index(color) for color in colors]

If you want to put that in a script run in batch, see here.

Upvotes: 0

Related Questions