Scheme: How to Loop GIMP Functions


For my first script of the new year, I started learning an entirely new language for an entirely different program that has resulted in my enduring a genuinely disturbing number of migraines over the last few days.

Tangential Warning: I don’t recommend scheme for beginners. I wouldn’t recommend it for any living creature because scheme is a monumental asshat of a language. It demands that you restructure the way you write math. Who the HELL decided THAT was a good idea? Oh, everyone in the world is accustomed to writing 2+2? Fuck those bastards, we’re going to force them to do +2 2 because programming is SUPPOSED to be confusing. And just to make it extra fun, EVERYTHING needs to go in parentheses. That way, even if the morons manage to get the expressions figured out, they’ll still have to spend HOURS guessing where the missing ) needs to go.

I’m not exaggerating. I just spent 6 hours writing scripts that don’t work. At one point, I wrote one so bad, it triggered an error in a plugin I didn’t even know I’d installed. The one script that did work happened accidentally. That’s what finally got me a loop in GIMP. Enjoy.

Hold up. What’s GIMP?

GIMP is a free image manipulation software. A nice alternative to Photoshop for us broke folks. It’s how I edit illustrations and make gifs. You can download it here.

Ok. What’s Scheme?

Scheme is the Dolores Umbridge of programming languages. It’s bureaucratic and passive aggressive, and if you don’t believe me, just wait till you’re flooded with unbound variable errors. It’s evil.

Also, it’s the language that GIMP speaks.

But How Do I Actually Talk to GIMP?

Through the Script-Fu console. It’s inside the Filters menu.

Gird your loins. It's all downhill from here.

If all has gone well, you’ll see a Script-Fu console window, and you can hit Browse to get to a magical window called the Procedure Browser. It’s a Gimp-To-Human dictionary that lists all the Gimp functions.

Behold the wonderful functions of Gimp.

Be not afraid. It looks technical, but really, it’s just telling you what you need to type into the console to make GIMP do its thing. I really only care about the parameters at this point because without those, the script won’t work.

What are Parameters?

They’re just extra bits you have to add so GIMP will know how to run the function. Type gimp-rect-select into the browser search, and several parameters pop up.

Oh my. Look at all those parameters.

The first parameter is image. It’s referring to a number assigned to your image, quite fortunately displayed at the top of the GIMP. The next four parameters are coordinates and measurements that can be found by hovering the cursor over the image and checking the bottom left hand corner.

Hey, that's not Ma Jolie.

Type that into the console, hit enter, and the result is a pretty thin rectangle selection of the top of the image. Now comes the painful part.

How Do I Plug That Into A Loop?

I don’t really care why it works at this point. I’ve just accepted that this particular template* is functional. Here’s the format:

(let loop ((variable StartValue))
(if (<variable EndValue)
(gimp-function parameters)
(loop (+variable Increment) ))))

*Based on this script on, courtesy of humblejohnny20.

So for my particular image, I want to add rectangular selections 5 pixels long every 10 pixels on the y axis. To do this, I’m plugging in a single y variable initially set to 0 which will increase in increments of 10 until it hits 750 (the length of the image). With each loop, GIMP will rectangle select an area 592 pixels wide (the width of the image) and add it to the existing selection because we specified Channel-Op-Add (0) in the parameters. Like so:

(let loop ((y 0))
(if (< y 750)
(gimp-rect-select 12 0 y 592 5 0 0 0)
(loop (+ y 10) ) ) ) )

If it works, GIMP will appear to have a mind of its own.

It's ALIVE!!!

Now if the top layer has an alpha channel, you can take the eraser and reveal horizontal stripes of the layer below.


Or you can create a striped .png to import into a Blender texture for a nice Happy New Year banner like the one I’ve got at the top. What else could this be useful for, you ask?

Absolutely no idea. I’m hoping my next round of god-awful scheme scripts will yield something more practical.


4 Replies to “Scheme: How to Loop GIMP Functions”

  1. I know this is 5 years old, but there is a reson why lisp-like languages use prefix notation for everything (because everything is a list). And they have the same amount of parenthesis as any other language, just on different places.

    also that sperging at the beggining is pretty funny

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: