What ImageMagick command will display the orientation of a JPG file?
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
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).
identify -format '%[EXIF:orientation]'
[To get exact metadata value.]identify -format '%[orientation]'
[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)