Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display the orientation of a JPEG file?

Tags:

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

like image 401
Mark Harrison Avatar asked Feb 21 '12 02:02

Mark Harrison


People also ask

How do I know the orientation of an image?

The length of the longest side determines the orientation. For example, if the height of the image is longer than the width, it is a “portrait” format. Images where the width is longer are called “landscape.”

How do you change the orientation of a picture?

Right-click the image and select Details to reveal a screen with metadata, including EXIF data, that you can adjust if the image supports it. Force a preferred orientation. Rotate the image, then save it. That process reconstructs the image along the requested dimensions.

How do I view EXIF orientation?

IrfanView. IrfanView is a great image viewer on Windows, which respects the image Exif info. To view the image Exif info, open an image and click Image -> Information . If the image contains Exif info, you can then click the EXIF info button at the bottom left of the popup window to check the image Exif info.


2 Answers

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)

like image 182
saurabheights Avatar answered Sep 27 '22 15:09

saurabheights


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).

like image 27
mathematical.coffee Avatar answered Sep 27 '22 16:09

mathematical.coffee