Rotating Images
To change the orientation of an image in IDL, use either the ROTATE or the ROT function. The ROTATE function changes the orientation of an image by 90 degree increments and/or transposes the array. The ROT function rotates an image by any amount and offers additional resizing options. For more information, see Using the ROT Function for Arbitrary Rotations.
Rotating an Image by 90 Degree Increments
The following example changes the orientation of an image by rotating it 270°.
Example Code
See rotateimage.pro in the examples/doc/image subdirectory of the IDL installation directory for code that duplicates this example. Run the example procedure by entering rotateimage at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT rotateimage.pro.
- Select the file and read in the data, specifying known data dimensions:
- Prepare the display device, load a color table, create a window, and display the image:
- Using the ROTATE syntax, Result = ROTATE (Array, Direction), rotate the galaxy image 270° counterclockwise by setting the Direction argument equal to
3. See ROTATE Direction Argument Options for more information. - Display the rotated image.
file = FILEPATH('galaxy.dat', $
SUBDIRECTORY=['examples', 'data'])
image = READ_BINARY(file, DATA_DIMS = [256, 256])
The following figure displays the original (left) and the rotated image (right).
ROTATE Direction Argument Options
The following table describes the Direction options available with the ROTATE function syntax, Result = ROTATE (Array, Direction).
|
Direction
|
Transpose?
|
Rotation Counterclockwise
|
Sample Image
|
|---|---|---|---|
| 0 |
No |
None |
|
| 1 |
No |
90° |
|
| 2 |
No |
180° |
|
| 3 |
No |
270° |
|
| 4 |
Yes |
None |
|
| 5 |
Yes |
90° |
|
| 6 |
Yes |
180° |
|
| 7 |
Yes |
270° |
|
Using the ROT Function for Arbitrary Rotations
The ROT function supports clockwise rotation of an image by any specified amount (not limited to 90 degree increments). Keywords also provide a means of optionally magnifying the image, selecting the pivot point around which the image rotates, and using either bilinear or cubic interpolation. If you wish to rotate an image only by 90 degree increments, ROTATE produces faster results.
The following example opens a image of a whirlpool galaxy, rotates it 33° clockwise and shrinks it to 50% of its original size.
Example Code
See arbitraryrotation.pro in the examples/doc/image subdirectory of the IDL installation directory for code that duplicates this example. Run the example procedure by entering arbitraryrotation at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT arbitraryrotation.pro.
- Select the file and read in the data, specifying known data dimensions:
- Prepare the display device and load a black and white color table:
- Create a window and display the original image:
- Using the ROT function syntax,
- Display the rotated image in a new window by entering the following two lines:
file = FILEPATH('m51.dat', $
SUBDIRECTORY = ['examples', 'data'])
image = READ_BINARY(file, DATA_DIMS = [340, 440])
Result=ROT(A,Angle, [Mag,X0,Y0] [,/INTERP] [,CUBIC=value{-1 to 0}] [, MISSING=value] [,/PIVOT])enter the following line to rotate the image 33°, shrink it to 50% of its original size, and fill the image display with a neutral gray color where there are no original pixel values:
arbitraryImg = ROT(image, 33, .5, /INTERP, MISSING = 127)
Your output should appear similar to the following figure.
The MISSING keyword maintains the original image's boundaries, keeping the interpolation from extending beyond the original image size. Replacing MISSING = 127 with MISSING = 0 in the previous example creates a black background by using the default pixel color value of 0. Removing the MISSING keyword from the same statement allows the image interpolation to extend beyond the image's original boundaries.

