Post Reply 

replace color in images, batch way

Jun 7, 2012, 16:45
Post: #1
replace color in images, batch way
Hi not sure if this fits in plugins section,

I need a simple script that takes a file, a color to replace and a color to replace it with.

and then just save it

I managed to get this out of tutorials and the procedure browser
(define (replace-color filename oldcolor newcolor)
  (let* ((image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
         (drawable (car (gimp-image-get-active-layer image))))
    (gimp-image-select-color image 2 drawable oldcolor)
    (gimp-channel-set-color (car (gimp-selection-save image)) newcolor)
    (gimp-file-save RUN-NONINTERACTIVE image drawable filename filename)
    (gimp-image-delete image)))
but this doesn't update the picture at all so I wonder what I might have missed.

the files are in image formats and not gimp formats, so I guess gimp-image-get-active-layer takes the only available layer (the background)

then we select all pixels of a certain color in this layer
and sets a new color on this selection channel

and finally save it.
gimp -i -b '(replace-color "/path/to/orangetest.png" "#E04613" "#0088FF")' -b '(gimp-quit 0)'
batch command executed successfully
Find all posts by this user
Quote this message in a reply
Sep 19, 2012, 18:15
Post: #2
RE: replace color in images, batch way
@ frazz
You could use Imagemagick on a command line.

For Gimp and a GUI then there is (again) Bimp (I don't get commission - honest)

The 'other gimp procedure' is 'plugin-exchange' equivalent to colours exchange in the colors map menu.
Except the scales are 0-255 rather than 0 - 1

A quick example using a couple of scribbles

If you look at that - change the red items to black, there are still some red pixels there.
maybe I should have used the exact red rather than full red or increased the threshold values.

Depends what you want to do. Worth a try.

** now answering questions**
Find all posts by this user
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  I need a way to quantify the color intensity of redness in a picture. marlohall 3 720 Jan 14, 2019 09:22
Last Post: paynekj
  creating multiple sized images from one ntuck86 1 617 Jul 29, 2017 08:18
Last Post: ythgilb
  Batch create images from array edmilner 1 814 Jul 17, 2017 16:41
Last Post: ythgilb
  How to close gimp with batch command? flixe 1 882 Jun 19, 2017 12:29
Last Post: ythgilb
  Script fu and Color to alpha john 4 1,496 Sep 20, 2014 08:46
Last Post: rich2005

Forum Jump:

GIMP ForumPortalArchiveContactTermsRSS