2016-08-22 00:56:27 +00:00
|
|
|
# Image utils
|
|
|
|
|
|
|
|
from io import BytesIO
|
2016-11-20 12:39:04 +00:00
|
|
|
from PIL import Image, ExifTags
|
2016-08-22 12:21:17 +00:00
|
|
|
# from exceptions import IOError
|
|
|
|
import PIL
|
|
|
|
from django.core.files.base import ContentFile
|
2016-08-22 00:56:27 +00:00
|
|
|
|
|
|
|
def scale_dimension(width, height, long_edge):
|
|
|
|
if width > height:
|
|
|
|
ratio = long_edge * 1. / width
|
|
|
|
else:
|
|
|
|
ratio = long_edge * 1. / height
|
|
|
|
return int(width * ratio), int(height * ratio)
|
|
|
|
|
2016-11-20 10:56:33 +00:00
|
|
|
def resize_image(im, edge, format):
|
2016-08-22 00:56:27 +00:00
|
|
|
(w, h) = im.size
|
|
|
|
(width, height) = scale_dimension(w, h, long_edge=edge)
|
|
|
|
content = BytesIO()
|
2016-08-22 12:21:17 +00:00
|
|
|
im = im.resize((width, height), PIL.Image.ANTIALIAS)
|
|
|
|
try:
|
|
|
|
im.save(fp=content, format=format.upper(), quality=90, optimize=True, progressive=True)
|
|
|
|
except IOError:
|
|
|
|
PIL.ImageFile.MAXBLOCK = im.size[0] * im.size[1]
|
|
|
|
im.save(fp=content, format=format.upper(), quality=90, optimize=True, progressive=True)
|
2016-08-22 00:56:27 +00:00
|
|
|
return ContentFile(content.getvalue())
|
|
|
|
|
2016-11-20 12:39:04 +00:00
|
|
|
def exif_auto_rotate(image):
|
|
|
|
for orientation in ExifTags.TAGS.keys() :
|
|
|
|
if ExifTags.TAGS[orientation]=='Orientation' : break
|
|
|
|
exif=dict(image._getexif().items())
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
return image
|