I am wondering if there are any tools (standalone, Lightroom/Photoshop plugins, or other) that can take a photo, look for lines that are almost vertical or horizontal (eg horizons, telephone poles, etc), and automatically rotate and crop the image?

This is a manual process for me right now, and it seems like something that could be automated.

Does anything like this exist?

  • 2
    \$\begingroup\$ I was thinking Image Magick could pull this off, but then I found this: wizards-toolkit.org/discourse-server/… \$\endgroup\$
    – BBischof
    Dec 7, 2010 at 6:35
  • 4
    \$\begingroup\$ Be aware that rotating your image even a few degrees is a destructive operation comparable to running a blur filter over your image. That may be fine, but it's better to practice getting your images straight in the first place. (And/or use a camera which can automatically rotate its sensor slightly to match a level.) \$\endgroup\$
    – mattdm
    Dec 7, 2010 at 14:49
  • \$\begingroup\$ if you use lanczos3 interpolation is it more like running a sharpening filter over it :) \$\endgroup\$ Feb 20, 2013 at 7:56
  • \$\begingroup\$ Original, bilinear,bicubic,lanczos2: sequoiagrove.dk/images/rotateinterp.jpg \$\endgroup\$ Feb 20, 2013 at 8:14
  • \$\begingroup\$ digikam seems to have a plugin for this digikam.org/node/445 \$\endgroup\$
    – chupvl
    Jan 2, 2016 at 20:56

5 Answers 5


There may well be programs that attempt to do what you describe, but I'm doubtful it would be that effective. If the camera is pitched up or down slightly then you could have a perfectly level image, despite not having a single horizontal or vertical line.

The reason for this is that unless your camera is dead flat along the optical axis (that runs parallel to the lens) then your telegraph poles etc. won't be vertical in an image, even if the camera is level - the lines will all converge on an imaginary vanishing point in the sky. Likewise, if the camera is not face on to a true horizontal line it won't be rendered horizontal in the image.

It's possible to employ a more sophisticated approach, by either trying to identify the horizon in images, or even better grouping lines that share a common vanishing point, estimating the pitch angle and thus the correct angle to rotate the image, but such a process would be considerably more involved.

  • \$\begingroup\$ How could an image processing program even know what is "level" \$\endgroup\$ Jul 16, 2012 at 2:10
  • \$\begingroup\$ @PatFarrell things like horizon, buildings, posts or signs, etc. I'd say it would be much easier than facial recognition or smile detection. \$\endgroup\$ Jul 17, 2012 at 5:32
  • \$\begingroup\$ Where I live, you can rarely see the horizon, too many trees. I would guess that 99% of my photos don't have a sign, post, etc. in them. \$\endgroup\$ Jul 22, 2012 at 4:24
  • \$\begingroup\$ Strange answer. This should be relatively trivial into today's ML world. The GoPro camera desktop software has auto-leveling of video. It does this post process with no extra info. There's no reason it couldn't do the same for photos. Training ML to level the horizon seems like a student level problem (ie, not hard at all). It doesn't need to see the horizon, it just needs samples that are level and samples that aren't. Googling found several examples like this \$\endgroup\$
    – gman
    Nov 22, 2020 at 0:33

I totally second what Matt Grum stated...trying to automatically "level" an image based on a purely logical algorithm would really only be effective in the ideal situation: Where you have a perfectly flat horizon in a properly centered image that minimizes lens and perspective distortions.

Consider the scenario where you, as the photographer, did take a level shot of something, such as a lake, that does not have a perfectly "horizontally flat" shore...the shore curves around and eventually meets you. A computer algorithm may try to level such an image by making the shore of the lake as flat as possible...but that is incorrect. The shore of the lake should be tilted and eventually curve toward you. The human eye can detect such a thing, as it involves numerous cues from the whole scene, not just primary lines. Small things, such as how "upright" trees look (which can be a very ephemeral thing that would be difficult for a computer algorithm to accommodate).

I think this is one of those good arguments for doing the best you can in-camera, before you take the shot, to make sure your shots are level. Beyond the technical difficulties of accurately leveling shots with an automatic algorithm, non-90 degree rotations are one of the most destructive edits you can make, as it requires re-sampling each pixel in the image. If you can take your shots in-camera such that they are properly leveled, you won't have to perform any rotations causing that degradation in image detail.


Yes, I haven't done it and some C or python programming is involved, but I'm going to try soon.

My situation is a camera mounted on a mast which is on a a buoy taking a picture which always contains the horizon. This should be a simpler application than yours.

First, look in to OpenCV. Specifically the Hough Line Transform.

For my case, I expect that the horizon will be so much more distinctive than any other line, I can tune it to only find one line. I can then rotate based upon the line's angle.

For your application, I think you would filter out any line's more than X degrees out of horizontal or vertical.

A little statistics, and you may be able to figure out how much to rotate.

As to degraded image quality, I'll be converting RAW files to ppm instead of jpeg, so there shouldn't be much beyond some cropping.


The Lightroom Develop module now has this as a built in option. No external programs needed. It can level either horizontally or vertically or, on full auto, will also compensate for diverging lines etc. It is reasonably good on images where the expected vertical and horizontal lines are obvious.


Google Photos https://photos.google.com/ can do this nowadays too. No RAW support and no batch processing though, and it only shows auto-level option when it feels like.

level option auto


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.