Search code examples
imagemagick

How to specify and read image's orientation


I. I don't understand how to specify image's orientation.

  • Step 1: I specify the image's orientation using

    magick 0.jpg -orient top-left 1.jpg
    
  • Step 2: I rotate the image using

    magick 1.jpg -rotate 90 2.jpg
    
  • Step 3: I try to restore the orientation I have specified earlier using

    magick 2.jpg -auto-orient 3.jpg
    

I expected that 3.jpg will be rotated -90 degrees, and so it will appear as 0.jpg and 1.jpg, but this doesn't happen, and it still appear as 2.jpg.

Am I doing something wrong?

II. Also, I don't understand how to find out the specified orientation. I tried

magick identify -format "%[orientation]" 3.jpg

but this returns Undefined.


Solution

  • You can set the orientation with exiftool like this:

    exiftool -orientation#=6 image.jpg
    

    Permissible numeric values are:

    1 = Horizontal (normal)
    2 = Mirror horizontal
    3 = Rotate 180
    4 = Mirror vertical
    5 = Mirror horizontal and rotate 270 CW
    6 = Rotate 90 CW
    7 = Mirror horizontal and rotate 90 CW
    8 = Rotate 270 CW
    

    Or you can use verbiage like this:

    exiftool -orientation="Rotate 90 CW" image.jpg
    

    If you want to retrieve the orientation, you can get it like this:

    exiftool -orientation image.jpg
    Orientation                     : Rotate 90 CW
    

    Or numerically with:

    exiftool -orientation# image.jpg
    Orientation                     : 6
    

    Or unadorned, like this:

    exiftool -s3 -orientation# image.jpg
    6
    

    You can equally retrieve it with ImageMagick, using escapes:

    magick image.jpg -format "%[orientation]" info:
    RightTop
    

    Or with ImageMagick using a JSON parser such as jq:

    magick image.jpg json: | jq -r '.[].image.orientation'
    RightTop
    

    Or with ImageMagick and awk:

    magick identify -verbose image.jpg | awk '/Orientation/ {print $NF}'
    RightTop