GIMP / image editors How to use math input in GIMP

One semi-hidden feature in GIMP is that you can do simple calculations in the spinbox widgets where you input numeric values. This tutorials explains how it works and all the advanced things you can do there.

So this recent repost on Twitter by the GIMP account (and originally by Andrei Rybak) blew up a little. Clearly, not everybody knew that you could do simple calculations right in the input fields for numbers. But… there’s actually more to tell than what you can see in that short video clip.

The text below is full transcript or the original video, with one single exception specifically pointed out later.

How math expressions calculations work

The general idea is that you should be able to do simple calculations right in the widget where you set some value.

So you place the editing cursor, for example, at the end of the current value, then write the arithmetic operation like minus for subtraction, add another value and then press the Tab key to get GIMP to calculate the result.

Math expression example

Now, there’s nothing wrong with training yourself to do all the calculations in your head. But if you are not that type of a person, you might find this quite useful.

And it is really not a new feature. It was added by Fredrik Alströmer in 2009 and then released three years later in version 2.8. Blender has had it for a number of years, I’m not even sure for how long exactly. Krita got it in 2016 thanks to Laurent Valentin Jospin. And even Photoshop got that in an update just a year ago, and I do believe it’s the last application in the Creative Suite to get that feature. Well, I guess that’s the corporate planning for you.

Some applications go even further. E.g. enve allows writing JavaScript code to make complex expressions, and it’s even animatable… But I’m digressing already, so that’s probably a story for another time.

Where it works

One important thing is that it doesn’t yet work everywhere. As of version 2.10.20, GIMP only supports basic expressions in the widget called spinbox. That’s the generic input field with two arrow buttons pointing up and down. You can find it in dialogs like Scale Image or in the Tool’s Settings dock.

Math expressions in tools settings dock

It doesn’t yet work in the slider widget which is where you would expect it to work. The team actually discussed this a while ago and is positive about having that. So all it needs is someone to write a patch.

It also doesn’t work in spinboxes in plugins, for a purely technical reason that, I believe, can also be remedied.

What expressions you can use

So what you can do is add:

Add

subtract:

Subtract

divide:

Divide

multiply:

Multiply

raise to Nth power:

Raise to Nth power

GIMP even respects the order of operations, so if you go for something like 600-2*5, the answer will be 590. Which means you can abuse GIMP to solve math quizzes online :)

You can also use parenthesis if you want to. So this kind of an expression

(200*3)*2+(4/2)

will yield the correct result which is 1202. Which is so much not a common use case but… what the hell!

Something I didn’t know about (so it didn’t make it into the video above) is that you can also use ratio expressions. Ell was kind enough to point that out.

Supposing you know the width of the image and you know you want it in 16:9 ratio. Put that width into the ‘Width’ box, then write ‘16:9’ in the ‘Height’ box and press TAB. There, you have it.

Using ratio expression in GIMP

Mixing units

Now if that wasn’t advanced enough for you, how about mixing units? You can do an expression like 10cm+3in, and you will get 17.619cm.

Simple mix of units

I’ll give you a very silly yet somewhat realistic example.

Supposing, you’re watching an American crime show, and someone reports a suspect who is 5 feet 4 inches tall. Now, if you are born and raised in the metric system, you are probably damned if you knew how much exactly that is. Well, fire up GIMP, press Ctrl+N for a new image, switch to centimeters, write 5ft + 4in and press Tab. Et voila!

Complex mix of units

Here is how it works. GIMP comes with a rarely used dialog called Units. This is pretty much a reference table for conversions between various length units that GIMP supports. This conversion system uses inches as the base unit. This is likely because resolutions in computer graphics are still commonly measured in inches.

Unit editor

Either way, for math expressions, you can use any of the units you can find in that dialog. You just need to reference a unit by its abbreaviated name. You can look it up in the Units dialog here, in this column.

You can even create your own unit, like a sea mile or a light year, and then mix it with feet or angstrems, if that’s where you fancies take you.

New unit

Future work?

Personally, I think this feature could be further improved in at least three ways.

The first one is adding support for math expressions to the slider widget. The second one is adding some kind of a hint that math expressions are possible in a widget. This would improve the discoverability of this feature. And finally, making more functions available. I’m primarily thinking here of triginometric functions like sine and tangent, some of the other applications like Krita already support that.

All three requests have been filed to the bug tracker. We’ll see if anyone is fond enough of the ideas to actually write the code.

Hopefully, you can make a slightly better use of GIMP now that you know all this.