Use EXIF info to fix orientation.

This commit is contained in:
Yuce Tekol
2012-12-26 19:26:40 +02:00
committed by Simon
parent 14224154f1
commit 01d6d5fe33

View File

@@ -34,6 +34,10 @@ from PIL import Image as PILImage
from PIL import ImageDraw, ImageOps
# EXIF specs Orientation constant
EXIF_ORIENTATION_TAG = 274
class Image:
""" Image container
@@ -48,6 +52,15 @@ class Image:
self.imgname = os.path.split(filename)[1]
self.img = PILImage.open(filename)
exif = self.img._getexif()
orientation = exif.get(EXIF_ORIENTATION_TAG)
# see: http://www.impulseadventure.com/photo/exif-orientation.html
rotate_map = {3: 180, 6: -90, 8: 90}
rotation = rotate_map.get(orientation)
if rotation:
self.img = self.img.rotate(rotation)
def save(self, filename, quality=90):
self.img.save(filename, quality=quality)