119

Canon DSLRs appear to save photos in landscape orientation and uses exif::orientation to do the rotation.

Question: How can imagemagick be used to re-save the image into the intended orientation using the exif orientation data such that it no longer requires the exif data to display in the correct orientation?

1

3 Answers 3

188

Use the auto-orient option of ImageMagick's convert to do this.

convert your-image.jpg -auto-orient output.jpg

Or use mogrifyto do it in place

mogrify -auto-orient your-image.jpg
Sign up to request clarification or add additional context in comments.

5 Comments

Don't forget you can use mogrify instead of convert if you want to replace the existing file (in-place), which is useful when you want to do a directory full.
Doesn't seem to work in all cases. I have at least a case that GIMP asks me if I want to fix the rotation, but convert just leaves the image as it is (leaving the real upper part of the picture in the right part).
If you’re into libvps: vips autorot your-image.jpg output.jpg See function lists: libvips.github.io/libvips/API/current/…
convert and mogrify can (and do) reduce your file size, and therefore your resolution.
Note that this will not update the ExifImageHeight/ExifImageWidth or rotate the internal thumbnail.
58

The PHP Imagick way would be to test the image orientation and rotate/flip the image accordingly:

function autorotate(Imagick $image)
{
    switch ($image->getImageOrientation()) {
    case Imagick::ORIENTATION_TOPLEFT:
        break;
    case Imagick::ORIENTATION_TOPRIGHT:
        $image->flopImage();
        break;
    case Imagick::ORIENTATION_BOTTOMRIGHT:
        $image->rotateImage("#000", 180);
        break;
    case Imagick::ORIENTATION_BOTTOMLEFT:
        $image->flopImage();
        $image->rotateImage("#000", 180);
        break;
    case Imagick::ORIENTATION_LEFTTOP:
        $image->flopImage();
        $image->rotateImage("#000", -90);
        break;
    case Imagick::ORIENTATION_RIGHTTOP:
        $image->rotateImage("#000", 90);
        break;
    case Imagick::ORIENTATION_RIGHTBOTTOM:
        $image->flopImage();
        $image->rotateImage("#000", 90);
        break;
    case Imagick::ORIENTATION_LEFTBOTTOM:
        $image->rotateImage("#000", -90);
        break;
    default: // Invalid orientation
        break;
    }
    $image->setImageOrientation(Imagick::ORIENTATION_TOPLEFT);
}

The function might be used like this:

$img = new Imagick('/path/to/file');
autorotate($img);
$img->stripImage(); // if you want to get rid of all EXIF data
$img->writeImage();

3 Comments

This solution works! I've only tried 1 image and there are of course 8, but I'll let you know how it goes. For me it was rotateImage, ->rotate just breaks
Thanks, fixed the rotateImage stuff. If you want to test all orientations: There is a neat github repo which has an image for each exif value.
thanks! worked in c#. i just needed a little conversion.
4

mogrify and convert both will lossy resize the image when correcting the orientation.

Use exiftran instead for lossless rotation.

On nice feature of exiftran is it's inplace flag (-i) allows you to process an entire directory at once, combined with the "auto" -a flag, like this:

exiftran -ai *.jpg

2 Comments

exiftran changed image dimensions 48x100 -> 50x100 on this input file: - mcc.id.au/2020/image-orientation/arrow-with-orientation.jpg
The image you provided is 100x50, and when I ran exiftran -ai arrow-with-orientation.jpg, it switched the orientation and dimensions were 50x100

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.