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
Code:
(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.
command:
Code:
gimp -i -b '(replace-color "/path/to/orangetest.png" "#E04613" "#0088FF")' -b '(gimp-quit 0)'
output:
Code:
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
http://registry.gimp.org/node/26259 (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 http://i.imgur.com/JaEHl.jpg

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.

** https://www.gimp-forum.net/ 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
  creating multiple sized images from one ntuck86 1 308 Jul 29, 2017 08:18
Last Post: ythgilb
  Batch create images from array edmilner 1 462 Jul 17, 2017 16:41
Last Post: ythgilb
  How to close gimp with batch command? flixe 1 457 Jun 19, 2017 12:29
Last Post: ythgilb
  Script fu and Color to alpha john 4 797 Sep 20, 2014 08:46
Last Post: rich2005
  Color menuitems in script or plugin Barbie 2 757 Sep 15, 2014 08:09
Last Post: rich2005

Forum Jump:


GIMP ForumPortalArchiveContactTermsRSS