Home > Web > Rotate images according to EXIF info

Rotate images according to EXIF info


Some digital cameras are saving the snapshots as they come from the digital sensor. This is not always the right orientation, as it depends on how you are position the camera. Luckily, the cameras are also saving the orientation information into EXIF attributes.
This article is about how to automatically correct this from your Java program, using jMagick. The entire thing can be break down into pieces: read an EXIF attribute for an image, interpret the value and transform the image.
The EXIF attribute responsible for orientation is called Orientation. According to EXIF specs, the values have the following meaning:

  • 1 = The 0th row is at the visual top of the image, and the 0th column is the visual left-hand side.
  • 2 = The 0th row is at the visual top of the image, and the 0th column is the visual right-hand side.
  • 3 = The 0th row is at the visual bottom of the image, and the 0th column is the visual right-hand side.
  • 4 = The 0th row is at the visual bottom of the image, and the 0th column is the visual left-hand side.
  • 5 = The 0th row is the visual left-hand side of the image, and the 0th column is the visual top.
  • 6 = The 0th row is the visual right-hand side of the image, and the 0th column is the visual top.
  • 7 = The 0th row is the visual right-hand side of the image, and the 0th column is the visual bottom.
  • 8 = The 0th row is the visual left-hand side of the image, and the 0th column is the visual bottom.
  • Other = reserved

Graphically, this looks like this: orientation.
The following transformations should be applied to the images according to their EXIF orientation:

Autorotate EXIF images

Autorotate EXIF images


where the possible operations are horizontal or vertical flip and rotate (90 degrees clockwise or counter-clockwise and 180 degrees). The corresponding jMagick methods from MagickImage class are:

  • horizontal flip – flip()
  • vertical flip – flop()
  • rotate 180 degrees – rotate(180)
  • rotate 90 degrees clockwise – rotate(90)
  • rotate 90 degrees counter-clockwise – rotate(-90)

Now let’s translate everything into Java code:

private static final int NONE = 0;
private static final int HORIZONTAL = 1;
private static final int VERTICAL = 2;
private static final int[][] OPERATIONS = new int[][] {
        new int[] {  0, NONE},
        new int[] {  0, HORIZONTAL},
        new int[] {180, NONE},
        new int[] {  0, VERTICAL},
        new int[] { 90, HORIZONTAL},
        new int[] { 90, NONE},
        new int[] {-90, HORIZONTAL},
        new int[] {-90, NONE},
        };

public static MagickImage rotateByExif(MagickImage image) throws MagickException {
    try {
        int index = Integer.parseInt(image.getImageAttribute("EXIF:Orientation")) - 1;
        int degrees = OPERATIONS[index][0];
        if (degrees != 0)
            image = image.rotateImage(degrees);
        switch (OPERATIONS[index][1]) {
            case HORIZONTAL:
                image = image.flopImage();
                break;
            case VERTICAL:
                image = image.flipImage();
        }
    }
    catch (NumberFormatException exc) {}
    catch (NullPointerException exc) {}
    return image;
}

Now you can simply call the rotateByExif method. For more information on how you can use jMagick to load and save images please see the official documentation or see Watermark with jMagick article. Actually you can even integrate this, so that you apply it to the watermarked images.

So actually it is pretty simple to rotate images according to the EXIF info using jMagick: just get the EXIF Orientation attribute with getImageAttribute() method and, according to its value, apply the rotateImage, flipImage and flopImage.

About these ads
Categories: Web Tags: ,
  1. VictorCharlie
    December 23, 2008 at 8:45 pm

    Thank you for educating me.

    I have used ImageMagick with python, from Linux, the bash shell and Java
    Process p = Runtime.getRuntime().exec(“convert imImage.jpg imImage.png”);

    Finding jMagick brings my focus back to java. I can just do java. And put java with other java.

    Thanks.

  2. Manderson
    September 25, 2009 at 5:19 am

    what a great site and informative posts, I will add a backlink and bookmark your site. Keep up the good work!

  3. Anonymous
    January 3, 2013 at 2:25 pm

    int index = Integer.parseInt(image.getImageAttribute(“EXIF:Orientati
    incomplete line
    how should use this one

    • January 4, 2013 at 2:40 am

      Actually the formatting was the problem. I updated the post.

  4. January 26, 2013 at 2:10 am

    Won’t the resulting MagickImage object still contain the original EXIF Orientation value? Don’t you need to update this Orientation field to the new value of “1″ to show that the image does not require any rotation?

  1. December 3, 2012 at 11:03 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: