Gimp-Forum.net
batch replace/swap colors in PNG images - Printable Version

+- Gimp-Forum.net (https://www.gimp-forum.net)
+-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP)
+--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP)
+---- Forum: Scripting questions (https://www.gimp-forum.net/Forum-Scripting-questions)
+---- Thread: batch replace/swap colors in PNG images (/Thread-batch-replace-swap-colors-in-PNG-images)



batch replace/swap colors in PNG images - dcraw999 - 05-12-2019

I have a few dozen PNG images in a folder. Each image has the same color palette (only 10 colors total). I am looking for a batch file to make 3 color changes to each image - here is the sequence of the changes:

1) replace ffffff with 00ff00
2) replace 000055 with ffffff
3) replace ffff00 with 000000

The changes have to be done in this order. I'm using GIMP 2.10.2 on Windows 10 64-bit. I installed BIMP, but I don't see a way to implement the color changes. I could run 3 batches too, one for each color change, or build a windows batch file to make all the changes, I'm just unsure of the GIMP code to accomplish the task.

Thanks for any help/direction.


RE: batch replace/swap colors in PNG images - Ofnuts - 05-12-2019

Why script? If the images aren't too big, you can

  1. load all the images as pieces of a single layer (sprite-sheet like),
  2. do the color change on the layer,
  3. split the layer back to files.
I have a several scripts for this that support two worflows, one where you create & save the work layer directly from the image files,  and one where you create the work layers from individual layers (that you mass-load/save). So

1) is either File>Open as layers followed by ofn-layer-tiles (Layer>Tiles>Join tiles), or just ofn-tiles (File>Open tiles)

2) is either ofn-layer-tiles (Layer>Tiles>Split tiles ...) followed by ofn-export-layers (File>Export all layers), or ofn-tiles (File>Export tiles)  

Second point, unless this is pixel art, smooth lines are rendered with pixels where the color is a mix of the colors on either side, so you may have more than 10 colors and you have to wonder what happens for these intermediate colors.
  • If you really have only 10 colors, then a simple way to do it is to make the image color-indexed and edit the colors  in the palette (in which case you don't even need to create a work layer, the palette is the same for all layers).
  • Otherwise the right way to do it is to convert the replaced color to transparency (bucket-fill with that color in Color erase mode) and then fill the transparent pixels with the target color by bucket-filling in Behind mode. This can be done quickly using the replace-foreground-by-background script.



RE: batch replace/swap colors in PNG images - dcraw999 - 05-13-2019

Ofnuts, thank you for the direction. I will try the color indexing method (edit the colors in the palette).