fixed EXIF rotations
This commit is contained in:
@@ -72,15 +72,35 @@ def must_be_csv(value):
|
||||
|
||||
def handle_uploaded_image(i):
|
||||
import StringIO
|
||||
from PIL import Image, ImageOps
|
||||
from PIL import Image, ImageOps, ExifTags
|
||||
import os
|
||||
from django.core.files import File
|
||||
image_str = ""
|
||||
for c in i.chunks():
|
||||
image_str += c
|
||||
imagefile = StringIO.StringIO(image_str)
|
||||
image = Image.open(imagefile)
|
||||
for chunk in i.chunks():
|
||||
image_str += chunk
|
||||
|
||||
imagefile = StringIO.StringIO(image_str)
|
||||
|
||||
|
||||
image = Image.open(i)
|
||||
|
||||
try:
|
||||
for orientation in ExifTags.TAGS.keys():
|
||||
if ExifTags.TAGS[orientation]=='Orientation':
|
||||
break
|
||||
exif=dict(image._getexif().items())
|
||||
print exif[orientation],'aap'
|
||||
|
||||
if exif[orientation] == 3:
|
||||
image=image.rotate(180, expand=True)
|
||||
elif exif[orientation] == 6:
|
||||
image=image.rotate(270, expand=True)
|
||||
elif exif[orientation] == 8:
|
||||
image=image.rotate(90, expand=True)
|
||||
except (AttributeError, KeyError, IndexError):
|
||||
# cases: image don't have getexif
|
||||
pass
|
||||
|
||||
if image.mode not in ("L", "RGB"):
|
||||
image = image.convert("RGB")
|
||||
|
||||
@@ -89,12 +109,12 @@ def handle_uploaded_image(i):
|
||||
hsize = int((float(image.size[1])*float(wpercent)))
|
||||
image = image.resize((basewidth,hsize), Image.ANTIALIAS)
|
||||
|
||||
|
||||
|
||||
filename = hashlib.md5(imagefile.getvalue()).hexdigest()+'.jpg'
|
||||
|
||||
filename2 = os.path.join('static/plots/',filename)
|
||||
image.save(filename2,'JPEG')
|
||||
|
||||
return filename,filename2
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user