MonScoDocEssai/app/scodoc/imageresize.py

34 lines
855 B
Python

"""Simple image resize using PIL"""
from PIL import Image as PILImage
try:
from io import StringIO # for Python 3
except ImportError:
from cStringIO import StringIO # for Python 2
def ImageScale(img_file, maxx, maxy):
im = PILImage.open(img_file)
im.thumbnail((maxx, maxy), PILImage.ANTIALIAS)
out_file_str = StringIO()
im.save(out_file_str, im.format)
out_file_str.seek(0)
tmp = out_file_str.read()
out_file_str.close()
return tmp
def ImageScaleH(img_file, W=None, H=90):
im = PILImage.open(img_file)
if W is None:
# keep aspect
W = int((im.size[0] * H) / float(im.size[1]))
im.thumbnail((W, H), PILImage.ANTIALIAS)
out_file_str = StringIO()
im.save(out_file_str, im.format)
out_file_str.seek(0)
tmp = out_file_str.read()
out_file_str.close()
return tmp