116

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

180

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
5
  • 16
    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.
    – zanedp
    Commented Jan 18, 2016 at 0:43
  • 1
    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).
    – xarlymg89
    Commented Apr 24, 2018 at 9:00
  • If you’re into libvps: vips autorot your-image.jpg output.jpg See function lists: libvips.github.io/libvips/API/current/…
    – MXDVL
    Commented Feb 9, 2021 at 20:09
  • convert and mogrify can (and do) reduce your file size, and therefore your resolution.
    – Dev Null
    Commented Apr 11, 2023 at 5:16
  • Note that this will not update the ExifImageHeight/ExifImageWidth or rotate the internal thumbnail. Commented Dec 30, 2023 at 22:17
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
  • 2
    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 Commented Aug 17, 2015 at 1:43
  • 3
    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.
    – tarleb
    Commented Aug 17, 2015 at 7:52
  • 2
    thanks! worked in c#. i just needed a little conversion.
    – Alvin
    Commented Jul 12, 2018 at 5:08
3

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
  • exiftran changed image dimensions 48x100 -> 50x100 on this input file: - mcc.id.au/2020/image-orientation/arrow-with-orientation.jpg
    – glen
    Commented Apr 27, 2023 at 13:14
  • The image you provided is 100x50, and when I ran exiftran -ai arrow-with-orientation.jpg, it switched the orientation and dimensions were 50x100
    – Dev Null
    Commented Jul 17, 2023 at 0:21

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.