Created
December 22, 2015 04:58
-
-
Save nacady/bf188c699e7e0bf57634 to your computer and use it in GitHub Desktop.
rotate photos based on exif orientation ref: http://qiita.com/nacady/items/1c57dc66b4af93f1d1b4
This file contains 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
from PIL import Image | |
import piexif | |
def rotate_img(img, rotate_param): | |
e = piexif.load(img.info["exif"]) | |
e['0th'][274] = 1 # no rotate | |
if rotate_param in [Image.ROTATE_90, Image.ROTATE_270]: | |
e['Exif'][40962], e['Exif'][40963] \ | |
= e['Exif'][40963], e['Exif'][40962] # swap width, height | |
img = img.transpose(rotate_param) | |
output = BytesIO() | |
img.save(output, 'JPEG', quality=90, exif=piexif.dump(e)) | |
length = output.tell() | |
output.seek(0) | |
return img, output, length | |
img = Image.open('file') | |
e = piexif.load(img.info["exif"]) | |
if self.exif and 'Orientation' in self.exif: | |
if self.exif['Orientation'] == 3: | |
img, output, length = rotate_img(self, img, Image.ROTATE_180) | |
elif self.exif['Orientation'] == 6: | |
img, output, length = rotate_img(self, img, Image.ROTATE_270) | |
elif self.exif['Orientation'] == 8: | |
img, output, length = rotate_img(self, img, Image.ROTATE_90) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment