24

What ImageMagick command will display the orientation of a JPG file?

apaderno
  • 28,547
  • 16
  • 75
  • 90
Mark Harrison
  • 297,451
  • 125
  • 333
  • 465

2 Answers2

28

Introduction

Images stores more than just pixel information. A lot of information is stored in form of metadata. Images can have multiple metadata in multiple metadata directories. Some examples are: Exif, IPTC, JFIF, Ducky, etc.

Orientation is one of such metadata tag in Exif directory. This metadata informs your display devices on how to orient an Image after decoding the pixel data. This metadata has valid values from 1-8. This metadata is not always present in Images, as one can remove the metadata from images. It is also possible to set this metadata to wrong values such as 0, 9, 17, etc, since this metadata supports 16-bit unsigned values(0-65535).

The image below shows how display of image is affected using this metadata. Image source: https://me94.me/2316.html

Affect of Orientation Metadata

Imagemagick Solution

Fetching orientation from Imagemagick(IM) can mean two things. You might be (mostly) interested in fetching what orientation the image has, and the second would be to know the exact value of orientation metadata tag(in rare cases, I assume).

To know the orientation, you thus have two IM commands(small variation in what they output).

  1. identify -format '%[EXIF:orientation]' <InputFileName> [To get exact metadata value.]
  2. identify -format '%[orientation]' <InputFileName> [To get image orientation value, a mapping of exif value to readable term.]

For command 1:-

The output is the exact value even when value may not be valid. For example:- 0,1,5 or even 65535. However, in the absence of this metadata, the output(IMHO ambiguous output) is: identify: unknown image property "%[EXIF:orientation]" @ warning/property.c/ InterpretImageProperties/3785.

For command 2:-

The output is as following:-

Undefined  - 0
Undefined  - [When no metadata]
TopLeft  - 1
TopRight  - 2
BottomRight  - 3
BottomLeft  - 4
LeftTop  - 5
RightTop  - 6
RightBottom  - 7
LeftBottom  - 8
Unrecognized  - any value between 9-65535, since 
                there is no mapping from value 9-65535
                to some geometry like 'LeftBottom'

Tested on Mac and Ubuntu(EC2)

Mark Harrison
  • 297,451
  • 125
  • 333
  • 465
saurabheights
  • 3,967
  • 2
  • 31
  • 50
  • This is the most correct answer. You either get the orientation from the image itself or from the EXIF tags. – pyb Sep 29 '17 at 18:56
22

You can use

identify -format '%[EXIF:Orientation]' <image.jpg>

as per identify -format docs (It's the bit further down about exif metadata).

Try

identify -verbose <image.jpg>

To see what metadata is in the image (for example if the image was not taken with a camera, the orientation tag will not be set).

Alternatively you could do something like

identify -format '%wx%h' <image.jpg>

which gives you the width by height (e.g. '800x598', '1936x2592') and use these to determine whether the image is upright or not (not sure how reliable this is though - sometimes you take a portrait image with a camera and the EXIF data will correctly record the orientation, but the image may still appear landscape).

Scott C Wilson
  • 19,102
  • 10
  • 61
  • 83
mathematical.coffee
  • 55,977
  • 11
  • 154
  • 194
  • Some Picture Viewers take care of how to show the image. To properly debug, remove exif metadata from image and then check if the image is actually rotated or not. – saurabheights Mar 07 '16 at 10:43
  • `identify -format '%[EXIF:Orientation]'` does not work for me with my tif files -- it just get a blank line. I have to use `identify -verbose ... | grep Orientation`. They won't let me take back my up-vote for this answer, so I'm leaving this comment. `Version: ImageMagick 6.7.8-9 2016-05-10 Q16` – Brian Tingle May 19 '16 at 18:39
  • 2
    correct solution for me was `identify -format "%[orientation]"` – Brian Tingle May 19 '16 at 18:47
  • @BrianTingle: Was this on mac? – saurabheights Oct 14 '16 at 07:18
  • 2
    @saurabheights this was on Amazon Linux; on OS X I get `identify: unknown image property "%[EXIF:Orientation]" @ warning/property.c/InterpretImageProperties/3760.` Amazon linux currently running Version: ImageMagick 6.7.8-9 2016-06-22 Q16 installed via yum and OS X running ImageMagick 6.9.4-3 Q16 x86_64 2016-05-19 installed via homebrew – Brian Tingle Oct 14 '16 at 22:10
  • @BrianTingle: Thank you for your response. I have opened a request for a universal format. It could be possible that this is a issue from homebrew, but highly unlikely, as argument parse code should be in IM. https://www.imagemagick.org/discourse-server/viewtopic.php?f=2&t=30685 – saurabheights Oct 15 '16 at 01:41
  • @BrianTingle: Both formats are valid. Please go through the request link. – saurabheights Oct 15 '16 at 04:04
  • @saurabheights look, I'm not trying to start any arguments or anything. I'm just saying that for me on OSX AND on AMAZON LINUX AMI **I** do not observe that `identify -format '%[EXIF:Orientation]'` works -- it does not return the value that I need to answer this question. For me `identify -format "%[orientation]"` is required to answer the question on OS X and on Amazon Linux AMI on the versions I listed. – Brian Tingle Oct 15 '16 at 15:32
  • 1
    @BrianTingle: My apologies, if I sounded rude. I didn't mean to offend at all. I was facing the same issue as you, so I thought to clear this up with ImageMagick developers/users, as it was inconvenient to me as well. Turns out "identify: unknown image property" does not mean that Imagemagick does not know exif:orientation, but that it didn't find this tag in image. Ambiguous response from IM. For more details, you can see my answer below. Again, I apologize, there was no objection made to you(I was in same boat as you) and it was completely unintentional. – saurabheights Oct 15 '16 at 17:10