Last active
January 3, 2018 04:24
-
-
Save paultopia/f579001f9a656c635ff09e980ea29edd to your computer and use it in GitHub Desktop.
fix unsupported exif jpeg image rotation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| some images get broken in desktop browsers because they're rotate with exif tags, which desktop browsers don't support, but ios does. | |
| (More specifically, neither desktop Chrome, Safari or Firefox on OSX as of 1/2/18 seem to respect exif tags when the image is embedded | |
| in a page, though at least Chrome respects them when the file is opened in its own tab.) | |
| see: https://www.howtogeek.com/254830/why-your-photos-dont-always-appear-correctly-rotated/ + | |
| https://stackoverflow.com/questions/42401203/chrome-image-exif-orienation-issue | |
| This leads to a weird situation where you can have a photo that was rotated in some exif-happy app that looks correct on ios but not | |
| on any desktop browser. Let's fix this. | |
| # this script takes a folder full of thus-broken images, strips the exif nonsense, and rotates each 90 degrees clockwise | |
| (which happens to be the problem I had. change line 39 to use a different rotation and see the man page for jpegtran). | |
| This tiny script is the equivalent of (from commandline, for each file): | |
| > exiv2 rm foo.jpg | |
| > jpegtran -rotate 90 foo.jpg > temp-foo.jpg | |
| > rm foo.jpg | |
| > mv temp-foo.jpg foo.jpg | |
| requires libjpeg (brew install jpeg) and exiv2 (brew install exiv2) | |
| """ | |
| from glob import glob | |
| from os import rename, remove | |
| from subprocess import call | |
| tempfiles = [] | |
| filenames = glob("*.jpg") | |
| for x in filenames: | |
| newname = "temp-" + x | |
| rename(x, newname) | |
| tempfiles.append(newname) | |
| for x in tempfiles: | |
| command = ["exiv2", "rm", x] | |
| call(command) | |
| oldname = x[5:] | |
| secondcommand = ["jpegtran", "-rotate", "90", x] | |
| with open(oldname, "wb") as outfile: | |
| call(secondcommand, stdout=outfile) | |
| remove(x) |
Author
Author
Favorite comment from the latter issue:
I also vote against the "image-orientation:from-image" property.
It would be like adding "buggy-behavior:false".
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Truly hopeless Chrome issues where this has been a known problem since 2010: https://bugs.chromium.org/p/chromium/issues/detail?id=56845 https://bugs.chromium.org/p/chromium/issues/detail?id=158753