Gimper - Python EZ Perspective 0.1
Gimper – Python EZ Perspective 0.1

Toelichting

Op 7 juni 2020 schreef ik over GIMP:

Gebouwen op foto’s rechtop zetten met het grafische bewerkingsprogramma GIMP is een lastige klus.
Er is gereedschap voorhanden waarmee je het perspectief kunt aanpassen, zodat convergerende lijnen van gebouwen die achterover lijken te vallen, weer parallel gezet kunnen worden. Op te roepen met <Shift><P> (onder Linux Ubuntu). Je kunt dan met de muis lijnen rechttrekken of de juiste getallen invoeren in het gereedschapsvenstertje. De werking van het gereedschap heb ik nooit precies kunnen doorgronden en de resultaten waren ook niet overtuigend. Gebouwen zagen er toch raar uitgerekt of verwrongen uit.
Maar gelukkig was er een script, geschreven door Average Joe #2 *) in de programmeertaal Python, dat deze klus kinderlijk eenvoudig maakte: Python EZ Perspective 0.1. De werking van het script wordt uitgelegd en ook de download en de installatie-instructie zijn via bovenstaande link te vinden.
(23-12-2020 – inmiddels loopt de link dood)

Het is jammer dat de link ons inmiddels het bos instuurt, want het was en is nog steeds een goed script, dat ook in de nieuwste versie van GIMP (op dit moment 2.10.18) met een paar stappen goed aan de praat te krijgen is. Hoe ik dat deed, beschreef ik in dezelfde blogpost. Wat het script vermag, werd door de ontwikkelaar Average Joe #2 *) goed beschreven (24-07-2014) en het is nog terug te vinden bij de WayBack Machine van The Internet Archive. Zie: WaybackMachine – Gimper.net – EZ Perspective 0.1. Om te voorkomen dat het ook daar op een dag verdwijnt, laat ik het hieronder in licht geredigeerde vorm volgen. Helaas heeft de WayBack Machine niet de bijbehorende illustraties gearchiveerd, zodat we het voorlopig zonder moeten doen. Misschien vind ik ze nog eens terug. Ik heb daarom nieuwe voorbeelden gemaakt en ervoor in de plaats gezet.

Het script kan bekeken en gekopieerd worden bij Github:

https://github.com/bzed/gimp-plugin-registry/blob/master/ez-perspective/ez-perspective.py

*) Average Joe #2 is vermoedelijk de nom de plume of nickname van wiskundige, ontwikkelaar en fotograaf Nils R. Barth. Zie nbarth.net.

Python EZ Perspective 0.1

Particularly well-suited for the needs of photographers seeking to correct perspective

Author: Average Joe #2
Date: July 24, 2014

Introduction

Turn this:

Amsterdam - Clematisstraat
Amsterdam – Clematisstraat

© Hans Konings

(ontbrekende oorspronkelijke afbeelding: bowl-and-board-original-scaled.jpg)

… into this:

Amsterdam - Clematisstraat
Amsterdam – Clematisstraat

© Hans Konings

(ontbrekende oorspronkelijke afbeelding: bowl-and-board-straightened-scaled.jpg)

… using the “EZ Perspective” filter, now available at Filters → Distorts → EZ Perspective…

Get ez-perspective.py now!

Particularly well-suited for the needs of photographers seeking to correct perspective, but flexible enough to be used by all those wishing to add perspective to an otherwise flat scene.

Download and Install

  1. Download ez-perspective.py.zip
  2. Unzip the ZIP file to get ez-perspective.py
  3. (maybe necessary) Make the .py file executable (on Linux and Mac OS X), via chmod +x ez-perspective.py (can also be done by right-clicking and changing “Properties”)
  4. Put in your plug-ins folder, which you can find at File → Preferences → Folders → Plug-Ins
  5. Restart GIMP
  6. Open a file, and find the filter in Filters → Distorts → EZ Perspective

Trouble?

If you’re having trouble:

  • Make sure that you’ve unzipped the file and that the extension is *.py
  • You might not have Python installed

Python 2 vs. Python 3

T/m versie 2.8 van GIMP werkte bovenstaande instructie nog. Met GIMP 2.10 werkt het niet meer, omdat deze versie de scripttaal Python 2 niet meer ondersteunt. Python 2 wordt door de ontwikkelaars van Ubuntu en GIMP als verouderd beschouwd, omdat Python 2 al enige tijd is opgevolgd door Python 3. Echter, op dit moment van schrijven doet ook Python 3 het niet in GIMP. Naar verwachting komt die ondersteuning er wel met GIMP 3.0. Gelukkig is er een methode om het verouderde Python 2 in de nieuwere versies van GIMP te integreren. Hoe dat te doen beschreef ik in EZ Perspective en GIMP 2.10.18. Als het script zichtbaar is in Filters → Vervormen → EZ Perspective en het venstertje zich opent, dan kan men aan de slag. Verder met de instructies van Average Joe #2…

Instructions

  • Use up/down to fix vertical lines not being parallel: \/ or /\ and use left/right to fix horizontal lines not being parallel: > or <. (And use rotate to rotate.) To get a feel for this, take photos of a window when tilting up, tilting down, swinging left, swinging right, and rotating clockwise/counter-clockwise, and then correct them.
  • Enter how much to change the angle in each direction, either by using the sliders or by explicitly keying in the angles in the boxes. (If a box is left at 0, the slider value is used.)
  • Easiest is to <TAB> between sliders, and use ←/→ (<LEFT>/<RIGHT>) to adjust each angle in turn: first up/down, then left/right, then rotation. (First make verticals parallel, then horizontals parallel, then rotate so actually vertical and horizontal. You may find tweaking rotation as you go helpful, rather than leaving it for last.)
  • Lacking preview, easiest way to try different angles via undo/redo (Ctrl-Z, Ctrl-Y) and re-run the filter via Ctrl-Shft-F.
  • You may find viewing a grid helpful (View → Show grid).
  • Use “fast” interpolation at first when zeroing in on best angle, then “good” for final version.
  • Oh, and as a technical matter, you’ll want to enter the 35 mm Equivalent Focal Length of the lens used (assuming this is a photograph). If this isn’t right, the perspective will be off (which is visible), and the angle measurements will be incorrect (which is just an interface issue).
  • You may find it convenient to refer to the directions as: up/down – “tilt” or “pitch”; left/right – “swing” or “yaw”; and rotation – “twist” or “roll”.
  • Use after correcting lens distortion (Filters → Distort → Lens Distortion… – barrel distortion, vignetting, lateral chromatic aberration), as these depend on the lens, the shot as taken.
  • Use before cropping, as this changes the shape of the image and you’ll likely want to crop afterwards. (Also, if you’ve cropped, you’ll have to adjust the EFL to compensate for the changed angle of view, and you’d really rather not fuss with that.)
  • In principle you can apply the filter repeatedly, and if you only rotate in one direction, angles are additive (5% + 5% = 10%). In practice this is undesirable and you should use a single transform because the filter interpolates each time it is applied, degrading image quality. More technically, if you rotate in more than one direction, the combined effect is not simply adding the angles, due to interactions between different rotations.

But wait, there’s more!

Actually, there isn’t. This is a specialized tool for easily correcting or changing perspective, just like it says on the tin, and that’s all it does.

Parallax errors

Parallax errors, which is usually undesirable (unless you want a very distorted effect). For example, if you take a shot of a colonnade from the left, the image does not contain the right sides of the columns, so if you change the perspective so it appears that the shot is taken from the front, the apparent “right side” of the columns will actually be the left sides of the columns, wrapped around. In people this is particularly visible in the nose looking very wrong.

  • Perspective is especially a concern when taking photographs of subjects in high places, which requires aiming up – unless you have a ladder, you can’t easily take the shot head-on. On the other hand, many such subjects are more familiar from ground level, so you may wish to retain perspective if you wish to emphasize height – it’s the difference between a shot of the second story looking like it’s from the second story across the street, or from street level. You also may wish to leave a little bit of distortion for buildings – to undercorrect – since it is expected and its absence looks a bit unnatural.
  • Left-right perspective is much less of a concern than up-down perspective – it is familiar and often adds depth and interest to an otherwise flat scene. Thus you may wish to retain it.
  • Minor rotation invariably looks like a mistake and should be corrected, unless you’re going for an intentionally unbalanced feel.
  • A 0.1% up/down or left/right move is quite small – at 10 mm EFL, it is just barely noticeable, and at longer focal lengths, it becomes v. small. (At a 50 mm “normal” length, it’s about 2–3 pixels at the edge of a 3600×2400 image, which is exactly what you’d expect – about a 0.1% move.) Slight tilt or swing is barely noticeable, especially at longer focal lengths (it’s more noticeable with wide-angle lenses), otherwise photography would require superhuman perfection, so don’t sweat it – 10% vs. 11% is not a huge difference.
  • Rotation (twist) is much more noticeable – a 1% error is very visible – and thus you’ll likely want to refine down to 0.1% or 0.2%.

Dutch angle

If you do this enough, your pictures will look like stills from Battlefield Earth!

Introduce parallax

If the subject is not flat, you will get parallax, which is a rather jarring distortion, but useful if you want a creepy look.

Selective focus

Take a shot with shallow DOF from one angle, then change perspective to another angle, thus having the plane of sharp focus at an angle, achieving Tilt-Shift lens-style effects. Note that if your scene is not all on one plane, you’ll have (possibly severe) parallax. Which you probably don’t want.

Don’t crop

Consider not cropping, or not cropping to rectangular – non-rectangular photos can be a dramatic change from conventional rectangular ones.

Perspective (search)

Perspective (search) do not actually do the desired perspective transforms. If these (or other) tools suit your needs, please use them, but they didn’t satisfy mine, and, if you read on, you, too, may come to share my cravings, and want a new tool. Fortunately, I’ve written one.

Simply, correcting perspective is a very common photographic task – like image rotation – and so you want a specialized tool for it. Further, it has hidden subtleties (elaborated below, in “Technical details”), which mean that a general tool is unlikely to provide the desired behavior.

Turning to the specific tools: the term “perspective tool”, while suggestive, is a misnomer. It is actually a “projective transformation” tool, and can do perspective transforms, but much else besides, and its transforms are almost always not simply changes of perspective. For minor changes, the error is slight, and not terribly objectionable, and many people advocate using this tool (or corresponding ones in other programs) to correct perspective.

Most often advocated is to fix perspective (say, from a shot that is tilted up) by simply stretching the top (e.g., here). The problem is that when you correct perspective, you also need to stretch vertically (or horizontally for left/right perspective), and if you don’t, the result is squat. Here is the result of simply stretching the top, without any vertical stretching (marked as incorrect):

Amsterdam - Clematisstraat
Amsterdam – Clematisstraat

© Hans Konings

(ontbrekende oorspronkelijke afbeelding: bowl-and-board-squat-correction-scaled.jpg)

Notice that it is squatter than the correct version; this is most visibly an error as shapes that should be circles are instead ovals.
Opmerking: Op mijn foto is dit zichtbaar aan de veel te platte letters van ‘Het Apostolisch Genootschap’ boven de ramen. Het script EZ Perspective doet dit veel beter. Zie slideshow hieronder. De eerste foto is het origineel, genomen met 24 mm brandpuntsafstand, de tweede bewerkt met GIMP’s eigen perspectiefgereedschap en de derde bewerkt met EZ Perspective.

Sony A7 III Zeiss 24 mm (detail)
Aangepast met GIMP Perspectief
Aangepast met EZ Perspective
Sony A7 III Zeiss 24 mm (detail)
Aangepast met GIMP Perspectiief
Aangepast met EZ Perspective
previous arrow
next arrow
 

© Hans Konings

Further, even if you want to stretch, how much should you stretch? For any angle (and focal length) there’s a single correct amount, mathematically determined, which is difficult to eyeball. Fortunately, we have computers that can do the math for us, as EZ Perspective does. It makes it, if you’ll pardon, “EZ PZ”. Lastly, you may wish a tool where you enter angles, rather than moving corners by mouse, as in the rotation tool. The perspective tool is very powerful and intuitive, but both overkill and a bit fiddly for simply fixing perspective.

Map Object – Plane

Map Object – Plane may appear to do the same thing, but it rotates the image, rather than the camera. This filter serves a different purpose – it uses an image as “wallpaper” on some surface. The easiest way to see that it does not do a perspective change is to rotate Y by 30° – the resulting perspective is compressed. In fact, if you rotate by 90°, the plane disappears, while if you actually change perspective by 90° (or, more realistically, by 80–85°), you still have a usable scene, albeit with heavy perspective, corresponding to shooting across (almost parallel to) a surface. More simply, Map Object rotates the image plane so that it is at an angle to the rendering plane, while changing perspective corresponds to projecting an image between parallelplanes.

Camera transform

Camera transform (or perhaps more properly, an inverse camera transform, followed by a camera transform) – it assumes that the center of the scene is some distance z* away, but the camera is aimed at an angle, and then changes that angle, keeping the center of the scene at the same distance. (Formally, this is PSO(3), acting on the plane given by z=z*.) The distance is why the 35 mm Equivalent Focal Length is necessary: the effect of changing angle depends on the distance – it determines both the relative scale of stretching (e.g., if tilting, how much vertical stretching) and the scale of angles. The easiest way to see this is to use a very short EFL on a photo taken with a longer EFL (e.g., use 10 mm for the example picture; actually taken with 89 mm) – the resulting corrected image will be too squat, and the correction angle keyed in will significantly understate the actual correct. Simply, tilting wide-angle lenses doesn’t stretch as much, and 1° change is a bigger change with a wide-angle lens than with a telephoto lens. As a detail, the EFL here refers to the diagonal EFL (based on diagonal angle of view), which is the most common convention (some use horizontal).

The rotations are done using the zyx convention for Euler angles (or more properly, Tait–Bryan angles), which can be interpeted as “rotate about the extrinsic axes (axes of the scene) in the order x, y, z (up/down tilt, left/right swing, rotation twist), or about the intrinsic axes (axes of the camera) in the order z, y, x.” This seems the most natural order – how would you line up a shot? This is also why applying the filter twice does not simply add angles – it corresponds to z′y′x′zyx (6 rotations in sequence), which is not the same as (z′+z)(y′+y)(x′+x) because the rotation group is not commutative (order of rotations matters).

Note that these camera transforms only have 6 degrees of freedom (it’s the Euclidean space group) – 3 rotations, 3 movements (2 dimensions of shifting the image, 1 dimension of scaling the image). Our concern are the 3 rotations, but the shifts and scaling show up subtly. The projective linear group of the plane, PGL(3) (all transforms that preserve lines, possibly going out to infinity), has 3×3–1 = 8 dimensions; note that the perspective tool has 8 degrees of freedom = 4 points × 2 dimensions/point, and hence is (a top-dimensional subset of) the full projective linear group. Examples of projective transforms that are not perspective are:

  • scaling in one dimension, but not in the other (“inhomogeneous scaling”)
  • shearing

It’s really hard to stay on a 6-dimensional space in 8 dimensions (it’s like staying on a 1-dimensional curve in 3 dimensions), which is why output from the perspective tool is almost never an actual perspective transform, and makes formal the fact that it’s hard to eyeball the right height fix. (It’s slightly subtler – different focal lengths add a 7th dimension, so it’s a bit easier to get a transform that’s a perspective transform, but likely for the wrong lens.) Further, only 3 of the dimensions correspond to rotations – the other 3 correspond to shifting and scaling, so unless you’re careful, the transformed image will be larger or smaller, and shifted: staying on a 3-dimensional space in 8 dimensions is trickier yet. For example, the usual “correct perspective by just stretching the top of an image” (say, by 5%) stretches the image horizontally by an average of 2.5%; to avoid stretching you actually would need to stretch the top by 2.5% and shrink the bottom by 2.5%. EZ Perspective corrects for these, as discussed below.

Projective transforms: shifting and scaling

Finally, two subtleties of projective transforms: shifting and scaling. Projective transforms (the matrices of the rotations) move the center of an image, and change the scale. This tool corrects for these by moving the center back to the center and adjusting scale to minimize distortions. This later is a bit tricky – projective transforms change scale differently in the x and y dimensions (and by a different amount in each row and column), so when changing the up/down tilt, we preserve the width of the center row, and correspondingly for left/right swing (preserve center column height). This is generally what you want; can be further customized by specifying where the “center” is, though I’ll add that later.

Further, changing the angle makes the distances that get closer to the camera grow, while those that get further shrink, so the former center of the image is no longer the center of the transformed image (even if it doesn’t move, there’re still more pixels on one side than on the other). This is inevitable, but changes the effect of the composition, and is a subtlety to be aware of.

Hope this tool proves useful – enjoy, and happy Gimping!

License: Commercial
GIMP Version: 2.6

links:

lees ook deel 1: EZ Perspective en GIMP 2.10.18