mirror of
https://github.com/ae-utbm/sith.git
synced 2024-11-14 02:03:22 +00:00
28 lines
918 B
Python
28 lines
918 B
Python
# Image utils
|
|
|
|
from io import BytesIO
|
|
from PIL import Image
|
|
# from exceptions import IOError
|
|
import PIL
|
|
from django.core.files.base import ContentFile
|
|
|
|
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)
|
|
|
|
def resize_image(im, edge, format): # TODO move that into a utils file
|
|
(w, h) = im.size
|
|
(width, height) = scale_dimension(w, h, long_edge=edge)
|
|
content = BytesIO()
|
|
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)
|
|
return ContentFile(content.getvalue())
|
|
|