111

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?

Nyxynyx
  • 61,411
  • 155
  • 482
  • 830
  • http://php.net/manual/en/function.exif-read-data.php Extract the rotation parameter, test it, rotate image if need be. – Marc B Oct 18 '13 at 17:59

3 Answers3

172

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
masterxilo
  • 2,503
  • 1
  • 30
  • 35
dlemstra
  • 7,813
  • 2
  • 27
  • 43
  • 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 Jan 18 '16 at 00: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 Apr 24 '18 at 09:00
  • If you’re into *libvps*: `vips autorot your-image.jpg output.jpg` See function lists: https://libvips.github.io/libvips/API/current/func-list.html#function-list – MXDVL Feb 09 '21 at 20:09
  • `convert` and `mogrify` can (and do) reduce your file size, and therefore your resolution. – Dev Null Apr 11 '23 at 05:16
57

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();
Tilo
  • 75
  • 2
  • 11
tarleb
  • 19,863
  • 4
  • 51
  • 80
  • 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 – Jack Franzen Aug 17 '15 at 01:43
  • 3
    Thanks, fixed the `rotateImage` stuff. If you want to test all orientations: There is a neat [github repo](https://github.com/recurser/exif-orientation-examples) which has an image for each exif value. – tarleb Aug 17 '15 at 07:52
  • 2
    thanks! worked in c#. i just needed a little conversion. – Alvin Jul 12 '18 at 05:08
2

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
Dev Null
  • 716
  • 1
  • 8
  • 20
  • `exiftran` changed image dimensions 48x100 -> 50x100 on this input file: - https://mcc.id.au/2020/image-orientation/arrow-with-orientation.jpg – glen Apr 27 '23 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 Jul 17 '23 at 00:21