Rotation / Orientation of iphone HEIC files

Started by SebC, March 09, 2025, 05:27:00 PM

Previous topic - Next topic

Phil Harvey

Hi Matti,

Thanks for running some tests, however you have managed to confuse me thoroughly.

I've compared the images IMG_6161.HEIC and IMG_E6161.HEIC (picture rotated by the iPhone), so I think that the image data itself has been rotated.  Does this explain the differences you are seeing?

Also, using -G3:1 would help clear because with the new pre-release version only one Rotation tag should be associated with the main image. 

For reference, below is a data map of an HEIC image that I made a while ago.  The "Rotation" tag is stored in the 'irot' box.  For this image there are 4 objects that refer to the same 'irot' box (the main full-sized 'grid', the preview-sized 'grid', the thumbnail 'hvc1', and the 'tmap').



IMG_6161.HEIC is substantially more complicated.  (Maybe I'll make a map of this if I have the time.)  For this image, there are 9 objects that refer to 'irot' boxes.  8 of these refer to the same 'irot' as the primary image -- this is the 'irot' that the ExifTool 13.25 pre-release writes.  The last 'irot' refers to the 'tmap' image.  Here is a quick breakdown:

ID  Type  'ispe'    'irot'  MIME
--- ------ --------- ------ ------
 46 'grid' 5712x4284  3     (primary item)
 62 'grid' 2856x2142  3     hdrgainmap
 63 'hvc1' 1024x768   3     linearthumbnail
 64 'hvc1' 2016x1512  3     semanticskinmatte
 66 'hvc1' 2016x1512  3     semanticskymatte
 68 'hvc1' 2016x1512  3     portraiteffectsmatte
 70 'hvc1' 416x312    3
119 'grid' 4096x3072  3     styledeltamap
122 'tmap' 4284x5712  0

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

wywh

Quote from: Phil Harvey on March 10, 2025, 01:27:15 PMThanks for running some tests, however you have managed to confuse me thoroughly.
Yes, I was afraid the report would be confusing to read. In some images the data itself might have been rotated so it is best to check plain vanilla images from the iPhone. Below are two such iPhone 16 Pro images which apps display correctly, here with G3:1 switch:

exiftool -a -G3:1 -s -Rotation -Orientation .
======== ./landscape.heic
[QuickTime]     Rotation                        : Horizontal (Normal)
[IFD0]          Orientation                     : Horizontal (normal)
[XMP-tiff]      Orientation                     : Horizontal (normal)
======== ./portrait.heic
[QuickTime]     Rotation                        : Rotate 90 CW
[Doc14:QuickTime] Rotation                      : Horizontal (Normal)
[IFD0]          Orientation                     : Rotate 90 CW
[XMP-tiff]      Orientation                     : Rotate 90 CW

- Matti

Phil Harvey

#17
Hi Matti,

Thanks.  So my supposition is not refuted?  (ie. ExifTool is rotating things correctly, and Apple utilities rotate the actual data?)

BTW, here is a map of the objects (black) and their properties (red) in IMG_6161.HEIC.  If you look carefully you can see the two 'irot' objects with differing rotation values.  Note that when ExifTool 13.25p rotates the image, it changes only the value stored in the 'irot' referenced by the primary image (46 grid in this case), so all other objects also using this property will be affected.  If the HEIC format seems like a pain in the ass... It is.



PH Edit: Updated HEIC map to show where ExifTool will add the top-level XMP.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

wywh

#18
Quote from: Phil Harvey on March 10, 2025, 04:11:18 PMExifTool is rotating things correctly
Yes, 13.25 (pre-release) seems to work correctly. It does not fix .heic that was rendered unreadable in older version, though.

I should have paid attention also to File:ImageWidth and File:ImageHeight when rotating via Preview.app which rotates data itself so File:ImageWidth and File:ImageHeight change places after 90° rotations and where Rotation & Orientation metadata is then always set to Horizontal (normal). GraphicConverter behaves the same. Sorry for the confusion.

Like exiftool, macOS Sequoia QuickLook or Quick Actions > Rotate Left rotate metadata but they are very flaky in doing so and the edited orientation very often fails to really stick in that iPhone 16 .heic. In older .heic and .jpg and movies they work OK and I often use QuickLook to losslessly rotate movies.

I tried to do a more thorough and hopefully not so confusing test:

As an example iPhone 16 IMG_6161.HEIC from the OP's message which displays correctly in portrait mode:

exiftool -a -G3:1 -s -ImageWidth -ImageHeight -Rotation -Orientation IMG_6161.HEIC
[File]          ImageWidth                      : 5712
[File]          ImageHeight                     : 4284
[QuickTime]     Rotation                        : Rotate 90 CW
[Doc10:QuickTime] Rotation                      : Horizontal (Normal)
[IFD0]          Orientation                     : Rotate 90 CW

...set the tags Horizontal (Portrait image tilted 90° to the left):

exiftool -m -P -overwrite_original -Rotation='Horizontal (normal)' -Orientation='Horizontal (normal)' IMG_6161.HEIC

exiftool -a -G3:1 -s -ImageWidth -ImageHeight -Rotation -Orientation IMG_6161.HEIC
[File]          ImageWidth                      : 5712
[File]          ImageHeight                     : 4284
[QuickTime]     Rotation                        : Horizontal (Normal)
[Doc10:QuickTime] Rotation                      : Horizontal (Normal)
[IFD0]          Orientation                     : Horizontal (normal)

...and back back to the 90° CW correct portrait position:

exiftool -m -P -overwrite_original -Rotation='Rotate 90 CW' -Orientation='Rotate 90 CW' IMG_6161.HEIC

exiftool -a -G3:1 -s -ImageWidth -ImageHeight -Rotation -Orientation IMG_6161.HEIC                   
[File]          ImageWidth                      : 5712
[File]          ImageHeight                     : 4284
[QuickTime]     Rotation                        : Rotate 90 CW
[Doc10:QuickTime] Rotation                      : Horizontal (Normal)
[IFD0]          Orientation                     : Rotate 90 CW

Then via Preview.app Rotate the portrait image 360° back to correct portrait position. It rotates data and drops off Doc10:QuickTime:Rotation while doing so:

exiftool -a -G3:1 -s -ImageWidth -ImageHeight -Rotation -Orientation IMG_6161.HEIC
[File]          ImageWidth                      : 4284
[File]          ImageHeight                     : 5712
[QuickTime]     Rotation                        : Horizontal (Normal)
[IFD0]          Orientation                     : Horizontal (normal)

- Matti

Phil Harvey

#19
Hi Matti,

Thanks.  Yes, ExifTool 13.24 and earlier deleted the duplicate Rotation tag when writing (without re-ordering the object properties, which was bad, and the reason it corrupted these images).  The new version won't fix images that have been previously corrupted in this way.

Rotating with Apple utilities (at least with the iPhone-rotated image posted here) not only deletes the duplicate Rotation tag (however it properly re-orders the object properties), but also it deletes many of the embedded images as well as manipulating the image data as we discussed.

I've just released 13.25 officially, and older versions should not be used to rotate these newer HEIC images.

Thanks to SebC for reporting this issue, and for the samples.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

SebC

Hello Phil,

Thanks for the great help, and thanks also Matti for the extensive testing and help.
I'll spend some time this week-end to test various configurations (horizontal / vertical pics, then various rotation / orientation settings) and see what happens.

Thanks all and take care !
Sébastien