Attachment | Size |
---|---|
ez-perspective.py (ZIP) | 5.61 KB |
ez-batch-script-SAMPLE.py (ZIP) | 1.23 KB |
Turn this:
…into this:
…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.
Demo video by Mahvin.
Installation :: Usage :: Use cases :: Tricks
Future plans :: Similar tools :: Technical details :: Batch processing :: Closing notes
ez-perspective.py.zip
ez-perspective.py
.py
file executable (on Linux and Mac OS X), via chmod +x ez-perspective.py
(can also do by right-clicking and changing “Properties”)Trouble?
If you’re having trouble:
.py
Some general tutorials on perspective correction: Perspective correction – PanoTools & Perspective Correction In Photoshop – Keystoning.
\/
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.(Ctrl-Z, Ctrl-Y)
and re-run the filter via Ctrl-Shft-F.
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.
Main desiderata is to have previewing, which makes tweaking perspective much easier. This requires building the GUI manually, using the full Python gimpplugin library, rather than using the pre-packaged one from gimpfu; it’s do-able, but will take a little time.
Also nice would be to automatically read EFL from Exif, rather than having to key it in. This can’t easily be done currently (Exif information is not available to plug-ins, as far as I can tell), though I plan to hack in something (using exiftool, if available).
“Hmm…,” you say, “but can’t I achieve the same using existing tools, like the (suggestively named) Perspective tool (Tool → Perspective, Shft-P
), or perhaps the ‘Map Object – Plane’ filter (Filters → Map → Map Object… → Plane)?”
Well, no.
These are both excellent and powerful tools, but they, and everything else I found discussed under “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):
Notice that it is squatter than the correct version; this is most visibly an error as shapes that should be circles are instead ovals.
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” 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 parallel planes.
For the mathematically-minded… These are all “as far as I can determine” – if there are any errors or you’ve thoughts or references, please feel free to contact me.
Formally, what this tool does is a “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:
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.
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.
You can also use EZ Perspective non-interactively, for batch processing, particularly if you have many files with known perspective, or wish to apply automated distortion. Because non-interactive use of Python-Fu plugins can be a little tricky, I’ve written a wrapper function to simplify it, and given an example script. See the below ez-batch-script-SAMPLE.py
file for how to do this; this will require some manual fiddling on your part. Please:
Lastly, the photo (corrected by 40% tilt and 0.3% clockwise rotation, at 89 mm EFL, following pincushion correction) is the former logo of Bowl and Board (hence the shape: a bowl on a board), a delightful housewares store in Cambridge, Mass., now sadly closed.
Hope this tool proves useful – enjoy, and happy Gimping!
Comments
small correction
I have Gimp 2.8.0, and it’s “Edit → Preferences → Folders → Plug-Ins” there,
not “File → …”
A note to Espy
Hi Espy.
If you have not yet solved your problem (which sounds similar to what I had when I tried to set up Python for Gimp 2.6.10 on Windows XP) you might try the following link that should take you direct to a tutorial that put me on the right track:
http://www.gimpusers.com/tutorials/install-python-for-gimp-2-6-windows.html
I uninstalled everything I had (unsuccessfully) tried to set up and then followed the tutorial step by step before re-installing Gimp and it all came together nicely for me.
Thanks for the tip
Thanks RonBu – that tutorial also worked for me, and I’ve linked it above.
A great tool
Thanks for a great tool. I've still to explore it's full potential but what I've seen so far indicates that this will have many uses for me.
Thanks!
Thanks for the kind words – if you think of any clever uses, please feel free to share them here!
Try forum for Python-Fu installation help
Hi ESPY,
EZ Perspective should do what you want (assuming I’ve got the math right)!
Sorry to hear about your Python-Fu installation troubles – I can’t help with that, but you may find the help forum useful (you’re more likely to find assistance there).
If you can’t get Python-Fu working, there are other programs with perspective correction (e.g., Hugin, Photoshop) – see Wikipedia article.
Hope these help!
Best,
Nils von Barth
Python-fu Installation Trouble
Before going in to my installation troubles, let me describe why I need EZ-perspective, just in case I'm on the wrong track here:
What I need EZ-perspective for so badly is that I need to do some true perspective transformations with correct scaling in *both* directions. I'm photographing a rectangular plane, sometimes at an angle, and I need to alter the images such that the image of the plane becomes truly rectangular, with the horizontal surfaces parallel with each other (as well as the vertical), all while keeping the ratio of the side lengths intact. Like trying to photograph a piece of paper at an angle and trying to determine its aspect ratio from the picture. Is it a4 or letter-sized?
Is EZ perspective even what I'm looking for? If so, please help me install Python-fu:
I'm having trouble installing the GIMP with Python support. I'm running GIMP 2.6.10 i686 on Windows 7 with Python2.6, PyGTK, PyGObject, PyCairo (all the latest releases, to the best of my knowledge). I've tried pretty much everything I can think of as well as the things I've read on various tutorials. I tried including the \bin folder from GTK+ into the PATH, I tried installing 'glade,' I've uninstalled, reinstalled, and rebooted more times that I can remember. Does anyone have any tips?
Thanks in advance
did you try to reinstall gimp
did you try to reinstall gimp ,with the custom option AFTER have installed all the python libraries ?
Retrieving lens data from the image
Hi,
I think it shoul dbe possible to use python-pyexiv2 (at least on linux) to retrieve the lens data from the image - that would make it easier on enter the correct values.
Otherwise a nice script, i plan to add it to the gimp-plugin-registry package in Debian :)
Thanks,
Bernd
pyexiv2? Great idea!
Hi Bernd,
Thanks for advising me of pyexiv2 – I wasn’t aware of it, and it seems the easiest solution (instead of hacking in exiftool). Turns out there are also Windows packages, so hopefully I’ll be able to get something working without too much work.
Including default EFL value will requires manually building the GUI, however, so I’ll work on that after I’ve moved to the manual GUI and done the more pressing task of having live preview!
Best,
Nils von Barth
Pages