I'm going to try to keep this short but I wanted to make a note of a quick script I just did to desaturate and lighten a bunch of images in the Gimp. The workflow I used was basically to open a bunch of images then do exactly the same thing to each one of them then close and save each one. My first thought was that clicking Tools->Colour Tools->Hue Saturation...
then doing an adjustment & hitting Okay was taking too long. I knew I'd been here before so I dug out the script I did before to do Hue Rotation in the Gimp
. I remember that painful learning curve to figure out Scheme. Luckily this time I want to use roughly the same command but I want to leave the hue alone and change the other two parameters. So I modified the code and pasted this in to the Script-Fu console.
(define (script-fu-adjust image )
(begin (gimp-selection-all image)
(gimp-hue-saturation (car (gimp-image-get-active-drawable image)) 0 0 20 -60)
(gimp-file-save 1 image (car (gimp-image-get-active-drawable image)) (car (gimp-image-get-filename image)) "")
The call to gimp-hue-saturation worked as I expected (see the previous post
for that); it raised the brightness by 20 and desaturated by 60. Not sure what the units are there but it made the image look right. The two new things here compared to the last time are
. I wanted to save and close each file after it was processed. I can't see a way to close files but
will save them. It wants the file name as a parameter. Maybe I could leave it blank or something but I don't know. I just looked up a function to get te existing file name since all my images already had names. That's what the
(car (gimp-image-get-filename image))
does. It gets the file name as a string. So this script processes the image and saves it but the * remains in the title and when you close it the Gimp will ask if you want to save. I just use alt-F4 to close the window then alt-N to answer the save question ("Don't Save"). This isn't optimal but it's quick.
This little script was nice, after pasting it in to the Script-Fu console, all I had to do was use the line
to process and save the image with the -70.0 as part of the name in the title bar. I wasn't satisfied or the script would get my work done too fast or something because I decided I wanted to process all the images in just one script. The thing is Scheme is a functional language and doesn't like loops. Instead they're all about recursion. I don't want to fight to make the language do something it doesn't want to do so I tried to write something that did the job recursively. Something was wrong with my code though and the error messages were useless.
In the end I found a little example on Wikipedia
that computes the Fibonnacci Sum for a number:.
(define (factorial n)
(define (iterate n acc)
(if (= n 0)
(iterate (- n 1) (* acc n))))
(if (< n 0)
(display "Wrong argument!")
(iterate n 1)))
I modified it until it handled my job:
(define (quicky a b)
(define (iterate a b)
(if (< a b)
(iterate (+ a 1) b)
(iterate a b)
It looks quite similar to the code I wrote on my own except that it works. After pasting this script in to the Script-Fu console, my workflow went like this:
- Open up a batch of pictures.
- Get the lowest and highest numbers that show up in the image title bars, for example 98.0 to 112.0
(quicky 98 112) in the Script-Fu console.
- Watch the computer do all the work.
It took a while to figure out, but my previous efforts helped me out so much that I think this one will come in handy again one day.