Fred's ImageMagick Scripts



    Licensing:

    Copyright © Fred Weinhaus

    My scripts are available free of charge for non-commercial use, ONLY.

    For use of my scripts in commercial (for-profit) environments or non-free applications, please contact me (Fred Weinhaus) for licensing arrangements. My email address is fmw at alink dot net.

    If you: 1) redistribute, 2) incorporate any of these scripts into other free applications or 3) reprogram them in another scripting language, then you must contact me for permission, especially if the result might be used in a commercial or for-profit environment.

    Usage, whether stated or not in the script, is restricted to the above licensing arrangements. It is also subject, in a subordinate manner, to the ImageMagick license, which can be found at: http://www.imagemagick.org/script/license.php

UNROTATE


Automatically unrotates a rotated image and trims the surrounding border.

Download Script

last modified: May 07, 2015



USAGE: unrotate [-f fuzzval] [-c coords] [-a angle] [-l left] [-r right ] [-t top ] [-b bottom ] infile [outfile]
USAGE: unrotate [-h or -help]

-f .... fuzzval .... fuzz value for determining border color;
.................... expressed as (float) percent 0 to 100;
.................... default=0 (uniform color)
-c .... coords ..... pixel coordinate to extract color; may be
.................... expressed as gravity value (NorthWest, etc)
.................... or as "x,y" value; default is NorthWest=(0,0)
-a .... angle ...... angle of rotation of image; default indicates
.................... to autocalculate; -45<=angle<=45 degrees (float)
-l .... left ....... pixel shift of left edge; +/- is right/left
.................... default=0 (no change)
-r .... right ...... pixel shift of right edge; +/- is right/left
.................... default=0 (no change)
-t .... top ........ pixel shift of top edge; +/- is down/up
.................... default=0 (no change)
-b .... bottom ..... pixel shift of bottom edge; +/- is down/up
.................... default=0 (no change)
[outfile] .......... if outfile is left off, the script will simply report
.................... the rotation angle needed to unrotate the image.

PURPOSE: To unrotate a rotated image and trim the surrounding border.

DESCRIPTION: UNROTATE computes the amount an image has been rotated and attempts to automatically unrotate the image. It assumes that the image contains a border area around the rotated data and that one must identify a coordinate within the border area for the algorithm to extract the base border color. A fuzz value should be specified when the border color is not uniform, but also because the edge of rotated image is a blend of image and border color. Thus, the fuzz value must be a compromise. If too large, the rotation angle will not be accurate. If too small, the blended edge around unrotated image will not be trimmed enough. The rotation angle displayed by the script for an appropriate fuzz value will typically be within a few tenths of a degree of the correct value. However, if the results are not accurate enough or there is still some border showing, you may rerun the script on the original image again and either specify an adjusted rotation angle or use the left/right/top/bottom arguments to specify extra trim.

ARGUMENTS:

-f fuzzval --- FUZZVAL is the fuzz amount specified as a percent 0 to 100 (without the % sign). The default is zero which indicates that border is a uniform color. Larger values are needed when the border is not a uniform color and to trim the border of the rotated area where the image data is a blend with the border color.

-c coords --- COORDS is any location within the border area for the algorithm to find the base border color. It may be specified in terms of gravity parameters (NorthWest, North, NorthEast, East, SouthEast, South, SouthWest or West) or as a pixel coordinate "x,y". The default is the upper left corner = NorthWest = "0,0".

-a angle --- ANGLE is the rotation angle needed to unrotate the picture data within the image. The default (no argument) tells the algorithm to automatically estimate the rotation angle. One may override the automatic determination and specify your own value. Values are positive floats between -45 and 45 degrees. Note that the algorithm cannot correct beyond 45 degrees and cannot distinguish between exactly +45 degrees and exactly -45. Therefore you may need to do a 90, 180, or 270 degree rotation after using this script. If the outfile is left off, then the script will simply report the rotation angle needed to unrotate the image.

-l left --- LEFT is the number of extra pixels to shift the trim of the left edge of the image. The trim is shifted right/left for +/- integer values. The default=0.

-r right --- RIGHT is the number of extra pixels to shift the trim of the right edge of the image. The trim is shifted right/left for +/- integer values. The default=0.

-t top --- TOP is the number of extra pixels to shift the trim of the top edge of the image. The trim is shifted down/up for +/- integer values. The default=0.

-b bottom --- BOTTOM is the number of extra pixels to shift the trim of the bottom edge of the image. The trim is shifted down/up for +/- integer values. The default=0.

CAVEAT: No guarantee that this script will work on all platforms, nor that trapping of inconsistent parameters is complete and foolproof. Use At Your Own Risk.


EXAMPLES


Unrotation Of Rotated Square Image

Rotated -45 deg

Rotated 45 deg

Rotated -20 deg

Rotated 10 deg

Rotated -5 deg

Rotated 2 deg

Arguments:
-f 35

Arguments:
-f 30

Arguments:
-f 30

Arguments:
-f 30

Arguments:
-f 30

Arguments:
-f 30

Rotation: 45.00o

Rotation: 45.00o

Rotation: 20.08o

Rotation: -9.62o

Rotation: 4.55o

Rotation: -2.60o



Unrotation Of -20o Rotated Square Image - Variation Of Fuzz Value

Original:
-20o

Arguments:
-f 0

Arguments:
-f 10

Arguments:
-f 20

Arguments:
-f 30

Arguments:
-f 40

Arguments:
-f 50

Rotation: 19.90o

Rotation: 19.94o

Rotation: 20.08o

Rotation: 20.08o

Rotation: 19.73o

Rotation: 20.19o



Unrotation Of -20o Rotated Square Image With Extra Border

Original:
-20o

Arguments:
-f 30

Rotation: 20.08o



Unrotation Of 10o Rotated Square Image With Extra Trim

Original:
10o

Arguments:
-f 30

Arguments:
-f 30 -l 1 -r -1 -t 1 -b -1

Rotation: -9.62o

Rotation: -9.62o



Unrotation Of Rotated Rectangular Image

Rotated -45 deg

Rotated -20 deg

Rotated 10 deg

Rotated -5 deg

Rotated 2 deg

Arguments:
-f 40

Arguments:
-f 40

Arguments:
-f 40

Arguments:
-f 40

Arguments:
-f 40

Rotation: 45.00o

Rotation: 19.91o

Rotation: -9.89o

Rotation: 4.80o

Rotation: -1.56o



What the script does is as follows:

  • Gets the color at the user specified coordinates
  • Pads the image with a 1 pixel border of that color
  • Converts the image to binary format using a fuzz fill
  • Averages the binary image to one row and thresholds at value 1
  • Averages the binary image to one column and thresholds at value 1
  • Determines the left/right trim coordinates from the
    transition from black to white pixel in the row image
  • Determines the top/bottom trim coordinates from the
    transition from black to white pixel in the column image
  • Subsections the binary image
  • Finds the left and top corners of the rotated area in the binary image
  • Computes the rotation angle of the edge between these corners
  • Rotates the image by the opposite angle
  • Converts the rotated image to binary format again (as above)
  • Determines the border around the image (as above)
  • Subsections the unrotated image to trim the border