Extract non-Exif orientation data from JPEG?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
pneuman
Posts: 4
Joined: 2013-07-17T17:21:14-07:00
Authentication code: 6789

Extract non-Exif orientation data from JPEG?

Post by pneuman »

Hi,

I have a JPEG that has orientation data in it, but it doesn't seem to be stored in the image's Exif data. If I run the image through convert with -auto-orient, it rotates the image, and "identify -verbose" is also able to extract the orientation:

Code: Select all

identify -verbose 0000221676_0.jpg | grep Orientation
  Orientation: RightTop
but neither of these commands produces any output when run:

Code: Select all

identify -format '%[EXIF:Orientation]' 0000221676_0.jpg
identify -format '%[IPTC:2:131]' 0000221676_0.jpg
The image in question is available here: http://wootangent.net/~lsd/blah/0000221676_0.jpg

I'm doing this as part of the image upload flow in a website, so I'd like to avoid the cost of running "identify -verbose" if possible. Is there another way to get the orientation information from images like this?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Extract non-Exif orientation data from JPEG?

Post by snibgo »

I'm not sure what you are asking. exiftool reports:

Code: Select all

XMP-tiff:Orientation='Rotate 90 CW'
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Extract non-Exif orientation data from JPEG?

Post by fmw42 »

IM does not find any EXIF Orientation data in the file. Though it should from the string formats at http://www.imagemagick.org/script/escape.php.

You can that it does not find it by

identify -verbose 0000221676_0.jpg

Code: Select all

  Properties:
    date:create: 2013-07-17T20:09:08-07:00
    date:modify: 2013-07-17T20:09:08-07:00
    dc:format: image/jpeg
    exif:ApertureValue: 4281/1441
    exif:ColorSpace: 1
    exif:DateTimeDigitized: 2012:06:13 17:54:28
    exif:DateTimeOriginal: 2012:06:13 17:54:28
    exif:ExposureProgram: 2
    exif:ExposureTime: 1/282
    exif:Flash: 24
    exif:FNumber: 14/5
    exif:FocalLength: 77/20
    exif:MeteringMode: 1
    exif:PixelXDimension: 2592
    exif:PixelYDimension: 1936
    exif:SensingMethod: 2
    exif:ShutterSpeedValue: 6845/841
    jpeg:colorspace: 2
    jpeg:sampling-factor: 1x1,1x1,1x1
    signature: 3aecead0329770055dc089c9c9c91a5ee25b599942f05a0f089e53d2170e8ea8
    tiff:Compression: 6
    tiff:JPEGInterchangeFormat: 656
    tiff:JPEGInterchangeFormatLength: 7441
    tiff:Make: Apple
    tiff:Model: iPhone 4
    tiff:XResolution: 72/1
    tiff:YCbCrPositioning: 1
    tiff:YResolution: 72/1
    xap:CreateDate: 2012-06-13T22:12:09Z
    xap:CreatorTool: Adobe Fireworks CS3
    xap:ModifyDate: 2012-06-13T22:12:09Z

However, the verbose information does show

Orientation: RightTop


I am not sure if IM needs the plain Orientation value or the EXIF Orientation value to autocorrect?

But running EXIFTOOL

exiftool -s -ee -g1 -u -n -D 0000221676_0.jpg

Code: Select all

---- ExifTool ----
    - ExifToolVersion                 : 8.71
---- System ----
    - FileName                        : 0000221676_0.jpg
    - Directory                       : .
    - FileSize                        : 135169
    - FileModifyDate                  : 2013:07:17 20:09:08-07:00
    - FilePermissions                 : 644
---- File ----
    - FileType                        : JPEG
    - MIMEType                        : image/jpeg
    - ImageWidth                      : 750
    - ImageHeight                     : 507
    - EncodingProcess                 : 0
    - BitsPerSample                   : 8
    - ColorComponents                 : 3
    - YCbCrSubSampling                : 1 1
---- JFIF ----
    0 JFIFVersion                     : 1 1
    2 ResolutionUnit                  : 1
    3 XResolution                     : 72
    5 YResolution                     : 72
---- XMP-x ----
    - XMPToolkit                      : Adobe XMP Core 4.1-c034 46.272976, Sat Jan 27 2007 22:37:37
---- XMP-tiff ----
    - Make                            : Apple
    - Model                           : iPhone 4
    - Orientation                     : 6
    - XResolution                     : 72
    - YResolution                     : 72
    - ResolutionUnit                  : 2
    - YCbCrPositioning                : 1
    - Compression                     : 6
    - JPEGInterchangeFormat           : 656
    - JPEGInterchangeFormatLength     : 7441
---- XMP-xmp ----
    - CreatorTool                     : Adobe Fireworks CS3
    - ModifyDate                      : 2012:06:13 22:12:09Z
    - CreateDate                      : 2012:06:13 22:12:09Z
---- XMP-exif ----
    - ExposureTime                    : 0.00354609929078014
    - FNumber                         : 2.8
    - ExposureProgram                 : 2
    - DateTimeOriginal                : 2012:06:13 17:54:28
    - DateTimeDigitized               : 2012:06:13 17:54:28
    - ShutterSpeedValue               : 0.00354715946612052
    - ApertureValue                   : 2.79999992194687
    - MeteringMode                    : 1
    - Flash                           : 24
    - FocalLength                     : 3.85
    - ColorSpace                      : 1
    - ExifImageWidth                  : 2592
    - ExifImageHeight                 : 1936
    - SensingMethod                   : 2
---- XMP-dc ----
    - Format                          : image/jpeg
---- Composite ----
    - Aperture                        : 2.8
    - Flash                           : 0
    - ImageSize                       : 750x507
    - ShutterSpeed                    : 0.00354609929078014
    - FocalLength35efl                : 3.85
shows Oriention: 6

So I am not sure why IM is not picking up the EXIF Orientation. It could be a bug.
pneuman
Posts: 4
Joined: 2013-07-17T17:21:14-07:00
Authentication code: 6789

Re: Extract non-Exif orientation data from JPEG?

Post by pneuman »

snibgo wrote:I'm not sure what you are asking. exiftool reports:

Code: Select all

XMP-tiff:Orientation='Rotate 90 CW'
Yep, exiftool does report the orientation, but for the sake of consistency, I want to extract the orientation using ImageMagick tools, so that the value I'm extracting is consistent with what "convert" uses when I later use -auto-orient when converting the image to another size.

A decription of our upload flow might help here. When a user uploads an image, we store the uploaded image as-is, and we also extract the width and height of the image, to store in our database. We also extract orientation (currently using "identify -format '%[EXIF:Orientation]'"), and if the orientation is one that involves a rotaion, we swap the width and height before storing them, so that the dimensions we store in our database match the image's true orientation.

When we serve the image on our site, we use resize the image to our desired size using convert, with the -auto-orient and -strip flags to re-orient the image and then remove its orientation data, if necessary. We need to know what size the image will be before we resize it, though, so that we can write the dimensions in to the page; the image is generated on-the-fly after the page has been rendered.

Right now, because we can't reliably extract the orientation, we sometimes display images with the wrong width and height in their img tags on the page. I could use another tool, like eximtool, to extract the orientation, but if there's ever a mismatch between what exiftool reports and what "convert -auto-orient" acts on, then I'll just end up back in the same situation.

I seems that I can extract the orientation using "identify -verbose", but this extracts a bunch of other things I don't need, and takes a noticeable amount of CPU time doing it, so I'd like to avoid it if possible.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Extract non-Exif orientation data from JPEG?

Post by fmw42 »

From http://www.imagemagick.org/script/comma ... uto-orient

"This operator reads and resets the EXIF image profile setting 'Orientation' and then performs the appropriate 90 degree rotation on the image to orient the image, for correct viewing."

I do not think that IM knows anything about the XMP-Tiff orientation that one finds with EXIFTOOL, though I am not sure. Thus it would have to be in the EXIF:Orientation field, which is not in your image.

I would suggest you post this issue to the Developers forum and inquire which Orientation value IM uses for -auto-orient. If IM knows about Orientation in the verbose information is that used when there is no EXIF:Orientation? Where does IM find this Orientation value when opening the file?

Although it it not listed as a string format at http://www.imagemagick.org/script/escape.php, this does work to get it:

convert 0000221676_0.jpg -format "%[orientation]" info:
RightTop

I will update the string format documentation tomorrow.
pneuman
Posts: 4
Joined: 2013-07-17T17:21:14-07:00
Authentication code: 6789

Re: Extract non-Exif orientation data from JPEG?

Post by pneuman »

fmw42 wrote:Although it it not listed as a string format at http://www.imagemagick.org/script/escape.php, this does work to get it:

convert 0000221676_0.jpg -format "%[orientation]" info:
RightTop

I will update the string format documentation tomorrow.
Thanks! That looks like exaclty what I'm after.

I might post about this on the developer forum, too, even if it's just to confirm what the expected behaviour is and make sure that the documentation is updated to match that behaviour.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Extract non-Exif orientation data from JPEG?

Post by fmw42 »

I might post about this on the developer forum, too, even if it's just to confirm what the expected behaviour is and make sure that the documentation is updated to match that behaviour.
I think that is a good idea -- to confirm what Orientation tag is being used by IM.
Post Reply