Episode 038: A Phython in a Barrel

icon for podpress  Podcast Video (48.1 MB) [36:10m]: Download (9040)
icon for podpress  Torrent: Play in Popup | Download (64)
icon for podpress  Python source code: Play in Popup | Download (47)

Alexanderplatz BerlinWill this title scare more people away than catch by curiosity? Who knows…..

The barrel distortion that gave me such problems last week is removed by using a built in filter for correcting (or simulating) lens distortions. You can find it in the image menu at Filters/Distorts/Lens Distortion.

As I have a lot of images with this error, I go out and write a special Python plugin to do this task. It’s not as complicated as it seems - I have a good template and the Web and Gimp give a lot of useful information. You can find the scripts used in this episode here on the Download Page

EDIT: If you are using Windows and GIMP 2.4.x, have a look here for informations about setting up Python for GIMP.
EDIT2: This is supposed to be even easier.

This is the finished Python program:

#!/usr/bin/env python

import math
from gimpfu import *

def remove_barrel_distortion(img, drw):

img.disable_undo()

layer_corr = drw.copy(True)
layer_corr.mode = NORMAL_MODE
layer_corr.name = "Barrel corrected"
img.add_layer(layer_corr, -1)

pdb.plug_in_lens_distortion(img, layer_corr, 0.0, 0.0, -12.0, 0.0, 0.0, 0.0)

img.enable_undo()
gimp.displays_flush

register(
"remove_barrel_distortion",
"Removes the barrel distortion of a Sanyo CA65",
"",
"Rolf Steinort <info @meetgimp.org>",
"public domain",
"2008",
"<image>/Filters/Distorts/B_arrel distortion removal CA65",
"RGB*, GRAY*",
[],
[],
remove_barrel_distortion)

main()

You find a similar program in this posting. There I have torn it apart and described all the parts. The Python for GIMP documentation and this website can be helpful.

Don’t forget the challenge! Make a monochrome image and post it in our meetthegimp.org photogroup at 23 and be sure to use the tag “mtg-monochrome”. The challenge ends March 31 1600GMT and I’ll draw a winner by random choice.

Contact me!

You can leave your comments on this blog or write me a mail.

Creative Commons License This work is licensed under a
Creative Commons Attribution-Share Alike 2.0 Germany License.

Tags: , , , , ,

21 Responses to “Episode 038: A Phython in a Barrel”

  1. Norman Says:

    The Python programming is interesting but rather over my head. I have tried to get to grips with Python but failed. Correction of lens distortion is covered as far as barrel distortion is concerned and next, what about perspective distortion. I think I know how to do it but confirmation by a demo from you could be helpful. I have several slide copies which show mild to quite severe perspective distortion because, at that time, I used a 28mm lens.

  2. Roberto Ballerini Says:

    You haven’t updated the download page (even if the zip file is available)

  3. Rolf Says:

    Downloadpage is updated, thanks for the hint.

    Perspective correction is not part of the lens correction. It doesn’t depend from the lens you use but from the angle between film/sensor plane and your subject. If you tilt it, you get these distortions. A correction is shown in the last show with the high rises at the Potsdammer Platz.

  4. Norman Says:

    That’s OK, what I wrote was with a little bit of tongue in cheek.

  5. Joseph Says:

    Brilliant introduction to programming (I am a programmer by trade)! I thought it was a stroke of genius to show a simple command with “an open mind” and not worry about all the other details. I think you may have whet many appetites to programming. Maybe you should mention some sites where you can download other peoples’ plugins and share your own creations as well as get help (the community is very helpful)?

  6. Serge Gielkens Says:

    A totally different show, let’s say GIMPro. I enjoyed it. This was my first introduction to Python and indeed, the way you showed it makes it look like a piece of cake.

    As Joseph said, maybe a Meet the GIMP! plugin repository would be helpful.

    As far as I am concerned I do not mind these more technical approaches to GIMP. But okay, I am biased: I am a programmer.

  7. Eric Says:

    Just a quick note, that #! on the very 1st line is no comment, it is a “shebang”, a special script direction to the interpreter unlike the usual # comments.

    http://en.wikipedia.org/wiki/Shebang_(Unix)

    Luv the show, Enjoy!!

  8. Rolf Says:

    @Eric: I know that, but this is a typical case of “too much information”. So I kept it low.

    @Joseph & Serge: I’ll look if there are such collections except the gimp.org repository. I think it would not be prudent to clutter that with such one-liners. But if something turns out to be good it should be put there additional.

    If I don’t find anything suitable I’ll set up a separate Blog at plugins.meetthegimp.org with a possibility to write articles for all.

  9. Eric Says:

    OK just checking :)

  10. Meet the GIMP « Ang Pilipino GIMP Says:

    [...] Episode 038: A Phython in a Barrel [...]

  11. Norman Says:

    I have been thinking about a comment Rolf made when replying to an earlier question of mine when he wrote that perspective distortion is not caused by the lens that is used. This surely is an over simplistic point of view. For example, a wide angle lens will give perspective distortion and what about a fisheye lens? I agree, that not having the camera level will accentuate the effect but there are examples where the same subject photographed near to with a wide angle lens shows distortion whereas photographed further away there is no distortion with a telephoto lens.

  12. jaims Says:

    Nice article, once again :-)

    Btw, I’m a programmer too (I’m focused on ms .net stuff, not for preferences but for my job’s characteristics, but I’ve learned some python along the way).

    The article is fine -among other things- because gets you to typing code in a smooth way, as you don’t have to be afraid of what programming is.

    Saludos

  13. steven Says:

    Simple question: How do I install Python under WinXP? I’ve followed the instructions founf here:
    http://www.gimpusers.de/news/2007-10-24/gimp-2-4-tarball-erschienen.html
    But when I install Gimp (Ver 2.4.5) the option for the Python extension is greyed out, nothing to choose :-( Any idea anybody?
    (Background: I’d like to try out this pythonfu-skript: http://www.gimptalk.com/forum/topic/python-fu-Dragan-effect-29852-1.html , may be the results are pure “kitsch”, let’s see.)

  14. Warren Says:

    Reply to Steve:

    Good tutorial. I know you are not using windows but the link below describes some of the troubles real people had installing Python into GIMP ( if that is the correct terminology.) I think you described the easy part of using Python in GIMP once installed. The hard part appears to be installing the software. Anyway thanks and maybe a simple script-fu tut. might be more useful since it already appears to be in GIMP.

    http://www.gimptalk.com/forum/topic/Python-And-Gimp-2-4-25587-1.html

  15. Raymond Says:

    Excellent tutorial. I find programming tough, your tutorial is very helpful.

    Cheers.

  16. Nick Says:

    Great Rolf! As a Python programmer myself, I have wanted to try this out, but being a Windows user I had never managed it. With the useful links you have provided I hope to be able to get it up and running. I hope to see more tutorials like this in the future.

  17. Bob Says:

    I created my first python test case and I can’t get through a “wrong parameter type” bug. Here’s the interesting part of my code:

    img.disable_undo()

    layer_ovoid = drw.copy(True) #make layer copy
    layer_ovoid.mode = NORMAL_MODE
    layer_ovoid.name = “Ovoid”

    img.add_layer(layer_ovoid, -1)

    pdb.plug_in_map_object( img,layer_ovoid,1,1,1,2,1,1,1,1,0,0,0,1,0,0,0,5,1,(255,255,255),-1,-1,2,-1,-1,1,1,1,1,0,27,True,False,False,True,1,0,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1 )

    img.enable_undo()
    gimp.displays_flush

  18. Rolf Says:

    I haven’t checked my answer against facts - so this is a wild guess. No GIMP on this machine.

    You have lots of integers (1 2 3) in your parameters. Perhaps some of them are floating point numbers. In this case you would have to write 1.0 instead of 1.

  19. Bob Says:

    I tried putting .0 on the ends of the integers and still no dice. I was hoping if the issue was float vs. integer that python would autoconvert the integers to floats. Either way this function is really starting to cause me pain. Should I post on Gimp Users or Gimp Developers?

    Thanks for the response, I have a hard time imagining how I could keep up with a site like this.

  20. Rolf Says:

    Have you checked which parameter is INT and which FLOAT?

    Gimp Users would be the right list, I think.

  21. David gowers Says:

    typically Python will coerce FLOAT INT, so in most cases that is a non-issue.
    You should split up that call over several lines so it is easier to read, and comment it too. like this:

    pdb.plug_in_map_object (img,layer_ovoid,
    # mapt vpx vpy vpz
    1, 1, 1, 2,
    # posx posy posz 1staxisx 1staxisy 1staxisz
    1, 1, 1, 1, 0, 0,
    # .. and so on..

    (spacing in the above may be incorrect)

Leave a Reply