Episode 038: A Phython in a Barrel

Alexanderplatz BerlinDownload the Video!
Download the companion file! (…if there is one…. ;-) )

Will 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.

The TOC

00:23 Update from the last episode
01:50 Lens Distortion
06:02 Comments on the blog
08:47 Programming in Python
09:40 – Discussion on the blog
10:56 – Explaining the Python program
20:33 – Procedure browser
25:31 – Parameters
27:35 – Error messages
29:30 – Adding a new layer
32:33 – Summary
34:01 “23″ image hosting
36:09 The End
TOC made by paynekj

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

28 thoughts on “Episode 038: A Phython in a Barrel

  1. 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. 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.

  3. 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)?

  4. 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.

  5. @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.

  6. Pingback: Meet the GIMP « Ang Pilipino GIMP

  7. 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.

  8. 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

  9. 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.)

  10. 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

  11. 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.

  12. 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

  13. 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.

  14. 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.

  15. 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)

  16. Hey Rolf!,

    I’ve been trying to create a gimp script with python to create a border, and I just cant figure it out. I’ve yet to add the actual border creating part of the script, but i DO have a script that I believe would create a new layer. I put my .py file into the gimp “docs” folder, and reloaded gimp, but nothing shows up. Here’s my script so far –

    #! /usr/local/bin/python

    import math
    from gimpfu import *

    def add_border(img, drw):

    img.disable_undo()

    #program

    layer_base = drw.copy(True)
    layer_base.mode = NORMAL_MODE
    layer_base.name = “Border”
    img.add_layer(layer_base, -1)

    img.enable_undo()
    gimp.displays_flush

    register(
    “add_border”,
    “Add a border to your text”,
    “Add in the color once script is run”,
    “Matt”,
    “public domain”,
    “2009″,
    “/Filters/Artistic/A_dd Border”,
    “RGB*, GRAY*”,
    [],
    [],
    add_border)

    main()

    I’m not really sure what’s up. Please help!!

  17. Have you set the file permissions to executable?

    I haven’t checked the code yet, no time at the moment and sitting on the wrong machine. But it looks OK at first glance.

  18. well, that’s one of the things. When I right click the .py file, I only get two tabs;General & Summary. I am runnign Python 2.5, and I’m not sure what’s happening.

  19. I think “import math” is not necessary here as you don’t do any python related math. As soon as you call pdb it will run gimp plugin only with parameters you provided.

    Math would be useful only when you decided to do some complicated calculations within your plugin using python.

  20. Pingback: Цифровая фотография - » MTG: Обзоры выпусков за весь 2008 год!

Anything to add from your side of the computer?