iPhone Photo Orientation/Rotation in Browser
I took the picture with iPhone 8+, when I posted it in my blog, it is rotated by 180 degree by default.
But if I view the post in Safari mobile, check the original picture in my phone or in Preview, or even open the image in a new Chrome tab, it looks perfectly normal, it just doesn’t work in a blog post in Chrome / Firefox / Safari (desktop).
After some investigation, I found that it’s all because the information hidden in EXIF. Let’s look at the EXIF data of the previous example, the Rotation field is actual noted as “rotated by 180 degree”, which means the photo itself is upside down.
But why it looks normal in my phone or in Preview?
I guess it’s because the image viewer Preview supports EXIF, it respects EXIF information and shows images in a correct way (rotate them if needed). On the contrary, popular web browsers (e.g. Chrome, Firefox, Safari) don’t support this.
After some more googling, this issue has been discussed back in 2010, here’s the bug filed for Chromium, there is also a plan to support EXIF rotation in
<img> tag by adding a property
image-orientation, unfortunately, it’s only supported in Firefox at the moment.
How to fix this?
There’s not much I can do to add the support to Chromium, there must be a reason it takes so long to support EXIF. However, since I have the full control of the image, I can rotate the image beforehand according to its EXIF data, so I don’t need to rely on web browsers to correctly show them.
filepath = '[FILE_PATH]'
- This is only a sample Python script used as a POC (prove-of-concept), please clean it up if you want to use it in Production.
- The script above will erase image EXIF data, so please use with caution, back up the image first if needed.
- The meaning of orientation value is in this table:
|EXIF Orientation Value||Row #0 is||Column #0 is|