0

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.

programc7r
  • 63
  • 6
  • If you include your Python code that wasn't working, perhaps someone can show you how to fix it. – martineau May 25 '20 at 23:21
  • Code works... but doesn't do what I want, I swapped color values in index 0 and index #. I did not change order of colors in map without affecting the image, which is the purpose, (particularly move a certain color to Index 0) like gimp does. – programc7r May 26 '20 at 09:06
  • Works but doesn't do what you want is a contradiction. Although I don't really understand what you're try to accomplish, I was hoping seeing your code would provide some insight into that. I'm fairly sure PIL can do it, whatever it is, if you can just convey what the goal is better. – martineau May 26 '20 at 11:20

1 Answers1

0

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.

xenoid
  • 8,396
  • 3
  • 23
  • 49
  • Good start but it tells too little to know what exactly I have to do. Here is an image if it helps what im trying. the arguments image,drawable, num colors, map is too unlcear for me to code https://gitlab.gnome.org/GNOME/gimp/uploads/b1783d3d725cde2db7aadf6a69aeda72/0GIMPfeature.jpg How would the code look for this process? Find e.g 255,0,255 (it different index # for each PNG) put it as first, do for each PNG that is opened, save each opened png? – programc7r May 27 '20 at 10:26
  • See augmented answer – xenoid May 27 '20 at 12:25