<p>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.</p>
<p>So <a href="https://twitter.com/GIMP_Official/status/1278781497613463552">this recent repost on Twitter</a> 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&hellip; there&rsquo;s actually more to tell than what you can see in that short video clip.</p>
<p>The text below is full transcript or the original video, with one single exception specifically pointed out later.</p>

<div style="position: relative; padding-bottom: 56.25%; height: 0; overflow: hidden;">
<iframe src="https://www.youtube-nocookie.com/embed/pPoGUxddGj8&quot; style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; border:0;" allowfullscreen title="YouTube Video" loading="lazy"></iframe>

<h2 id="how-math-expressions-calculations-work">How math expressions calculations work</h2>
<p>The general idea is that you should be able to do simple calculations right in the widget where you set some value.</p>
<p>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 <strong>Tab</strong> key to get GIMP to calculate the result.</p>
<p><img src="math-expr-explanation.gif" alt="Math expression example"></p>
<p>Now, there&rsquo;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.</p>
<p>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&rsquo;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&rsquo;s the last application in the Creative Suite to get that feature. Well, I guess that&rsquo;s the corporate planning for you.</p>
<p>Some applications go even further. E.g. enve allows writing JavaScript code to make complex expressions, and it&rsquo;s even animatable… But I&rsquo;m digressing already, so that&rsquo;s probably a story for another time.</p>
<h2 id="where-it-works">Where it works</h2>
<p>One important thing is that it doesn&rsquo;t yet work everywhere. As of version 2.10.20, GIMP only supports basic expressions in the widget called spinbox. That&rsquo;s the generic input field with two arrow buttons pointing up and down. You can find it in dialogs like <em>Scale Image</em> or in the Tool&rsquo;s Settings dock.</p>
<p><img src="math-expr-tool-settings.jpg" alt="Math expressions in tools settings dock"></p>
<p>It doesn&rsquo;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.</p>
<p>It also doesn&rsquo;t work in spinboxes in plugins, for a purely technical reason that, I believe, can also be remedied.</p>
<h2 id="what-expressions-you-can-use">What expressions you can use</h2>
<p>So what you can do is add:</p>
<p><img src="math-expr-gimp-add.png" alt="Add"></p>
<p><img src="math-expr-gimp-subtract.png" alt="Subtract"></p>
<p><img src="math-expr-gimp-divide.png" alt="Divide"></p>
<p><img src="math-expr-gimp-multiply.png" alt="Multiply"></p>
<p>raise to Nth power:</p>
<p><img src="math-expr-gimp-power.png" alt="Raise to Nth power"></p>
<p>GIMP even respects the order of operations, so if you go for something like <code>600-2*5</code>, the answer will be <code>590</code>. Which means you can abuse GIMP to solve math quizzes online :)</p>
<p>You can also use parenthesis if you want to. So this kind of an expression</p>
<p>will yield the correct result which is <code>1202</code>. Which is so much not a common use case but&hellip; what the hell!</p>
<p>Something I didn&rsquo;t know about (so it didn&rsquo;t make it into the video above) is that you can also use ratio expressions. Ell was kind enough to point that out.</p>
<p>Supposing you know the width of the image and you know you want it in 16:9 ratio. Put that width into the &lsquo;Width&rsquo; box, then write &lsquo;16:9&rsquo; in the &lsquo;Height&rsquo; box and press TAB. There, you have it.</p>
<p><img src="math-expr-gimp-ratio.gif" alt="Using ratio expression in GIMP"></p>
<h2 id="mixing-units">Mixing units</h2>
<p>Now if that wasn&rsquo;t advanced enough for you, how about mixing units? You can do an expression like <code>10cm+3in</code>, and you will get <code>17.619cm</code>.</p>
<p><img src="math-expr-mix-units-simple-example.gif" alt="Simple mix of units"></p>
<p>I&rsquo;ll give you a very silly yet somewhat realistic example.</p>
<p>Supposing, you&rsquo;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 <em>that</em> is. Well, fire up GIMP, press <strong>Ctrl+N</strong> for a new image, switch to centimeters, write <code>5ft + 4in</code> and press <strong>Tab</strong>. Et voila!</p>
<p><img src="math-expr-mix-units-complex-example.gif" alt="Complex mix of units"></p>
<p>Here is how it works. GIMP comes with a rarely used dialog called <em>Units</em>. 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.</p>
<p><img src="math-expr-units-dialog.png" alt="Unit editor"></p>
<p>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.</p>
<p>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&rsquo;s where you fancies take you.</p>
<p><img src="math-expr-units-new-unit.png" alt="New unit"></p>
<h2 id="future-work">Future work?</h2>
<p>Personally, I think this feature could be further improved in at least three ways.</p>
<p>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&rsquo;m primarily thinking here of triginometric functions like sine and tangent, some of the other applications like Krita already support that.</p>
<p>All three requests have been filed to the bug tracker. We&rsquo;ll see if anyone is fond enough of the ideas to actually write the code.</p>
<p>Hopefully, you can make a slightly better use of GIMP now that you know all this.</p>