Gamma-correct image scaling 2011-05-09

I recently heard of Eric Brasseur’s article Gamma error in picture scaling. The news that many graphics tools including the GIMP hurt certain colors during image scaling was rather surprising to me.

I was actually looking a command line tool to that job right out of the box — any recommendations besides ImageMagick/convert or netpbm/pamscale? — but I ended up writing a pygimp-based GIMP plugin yesterday. It does the job and comes with a simple GUI. Compared to core GIMP, the speed is quite disappointing though, probably because of using Python (and an extra layer) but C code.

Once you have the plugin installed

# sudo layman -a betagarden
# sudo emerge -av media-plugins/gimp-imagescaler

you should find an item “Scale Image with correct Gamma…” at the bottom of the “Image” menu.

The code is free software, clean patches are welcome:

10 Comments
Pavel May 10th, 2011

Hmm. Makes me wonder, if there’s basically a fix in the form of a plugin, why isn’t it implemented in gimp’s scaler itself? either way, thanks for sharing, I didn’t know about this at all.

sping May 10th, 2011

Valid question. Let me reply by pointing to this GIMP bug report:
https://bugzilla.gnome.org/show_bug.cgi?id=168022

Pavel May 10th, 2011

Thanks 🙂 hopefully this gets into GIMP itself. GEGL’s scheduled to arrive in a looong time, so having a working implementation in 2.6, heck even 2.7 would be great.

Jure May 11th, 2011

How about media-gfx/imageworsener?

sping May 12th, 2011

Actually that tool was what lead me to the article in the first. I lost the name of the tool somehow and were actually looking for it since then. So thanks for bringing it back!

Simon May 17th, 2011

Yeah, OS graphics is in a sad state. Even if GIMP manages it, e.g. XRender impls usually don’t respect this issue. The only tool I discovered so far is (tadaaa!…)

http://netpbm.sourceforge.net/doc/pamscale.html

That GIMP bug is exemplary of the widespread confusiuon surrounding the issue – I’m working on some graphics-related project and no one has a clue. It’s not set to improve soon.

sping May 17th, 2011

pamscale is part of netpbm, which I mentioned in the post. I will change it to netpbm/pamscale now.

Have you checked media-gfx/imageworsener? It seems to do the right thing.

Simon May 17th, 2011

Ah, sorry I assumed people naturally turn to pnmscale when they talk about netpbm – which of course is broken.

WRT imageworsener – that is really a cool tool. But as it happens I wrote a cool tool myself (diploma thesis) and so I never really had the pressure to look for imageworsener or the like.

Ikem Krueger January 17th, 2015

> Even if GIMP manages it, e.g. XRender usually don’t respect this issue.

Maybe we should report that upstream?

(Where would I do that?)

BASTA! January 23rd, 2015

Happens every time when using cubic interpolation:

Traceback (most recent call last):
File “/usr/lib/gimp/2.0/python/gimpfu.py”, line 735, in response
dialog.res = run_script(params)
File “/usr/lib/gimp/2.0/python/gimpfu.py”, line 361, in run_script
return apply(function, params)
File “/usr/lib/gimp/2.0/plug-ins/imagescalerplugin.py”, line 32, in process
scale_image(img, width, height, interpol, gamma, GimpEnv())
File “/usr/lib/python2.7/site-packages/imagescaler/main.py”, line 86, in scale_image
scale_copy_layer(dst_layer, layer_backups[i], interpol, gamma, env, i, layer_count, cache_exp2linear)
File “/usr/lib/python2.7/site-packages/imagescaler/main.py”, line 64, in scale_copy_layer
rgba = f(src_region, src_layer, sx, sy, xfrac, yfrac, gamma, cache_exp2linear)
File “/usr/lib/python2.7/site-packages/imagescaler/algorithms/cubic_gimp.py”, line 40, in interpolate_cubic_gimp
p0 = cubic_spline_fit(xfrac, s0[3], s0[7], s0[11], s0[15])
IndexError: list index out of range

Happens every time when using bi-linear interpolation:

Traceback (most recent call last):
File “/usr/lib/gimp/2.0/python/gimpfu.py”, line 735, in response
dialog.res = run_script(params)
File “/usr/lib/gimp/2.0/python/gimpfu.py”, line 361, in run_script
return apply(function, params)
File “/usr/lib/gimp/2.0/plug-ins/imagescalerplugin.py”, line 32, in process
scale_image(img, width, height, interpol, gamma, GimpEnv())
File “/usr/lib/python2.7/site-packages/imagescaler/main.py”, line 86, in scale_image
scale_copy_layer(dst_layer, layer_backups[i], interpol, gamma, env, i, layer_count, cache_exp2linear)
File “/usr/lib/python2.7/site-packages/imagescaler/main.py”, line 64, in scale_copy_layer
rgba = f(src_region, src_layer, sx, sy, xfrac, yfrac, gamma, cache_exp2linear)
File “/usr/lib/python2.7/site-packages/imagescaler/algorithms/bilinear_gimp.py”, line 38, in interpolate_bilinear_gimp
alphasum = weighted_sum(xfrac, yfrac, p1[3], p2[3], p3[3], p4[3])
IndexError: list index out of range

In addition, this message box is shown after the python-fu-imagescaler dialog is cancelled after the above errors:

Plug-In ‘Scale Image with correct Gamma’ left image undo in inconsistent state, closing open undo groups.

Leave a Reply

You must be logged in to post a comment.