Skip to content

Instantly share code, notes, and snippets.

@cherian
Created December 18, 2012 06:36
Show Gist options
  • Save cherian/4325597 to your computer and use it in GitHub Desktop.
Save cherian/4325597 to your computer and use it in GitHub Desktop.
from PIL import Image
class CTImage(object):
def rotate(self, degree):
self._img = self._img.rotate(degree, expand=True)
(self.width,self.height) = self._img.size
return self
def orientation(self):
exif = self._img._getexif()
if exif is None:
return 0
orientation = 0x0112
return exif[orientation]
def orientation_fix(self):
'''
Fixes device orientation
'''
device_pos = self.orientation
if device_pos == 3 :
self.rotate(180)
return True
if device_pos == 6 :
self.rotate(270)
return True
if device_pos == 8 :
self.rotate(90)
return True
return False
if ctimage.orientation_fix():
instance = image.instance
instance.original_width = ctimage.width
instance.original_height = ctimage.height
instance.original_orientation = ctimage.orientation
instance.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment