ScoDoc-PE/app/scodoc/imageresize.py

31 lines
792 B
Python
Raw Normal View History

2020-09-26 16:19:37 +02:00
"""Simple image resize using PIL"""
2021-07-09 13:59:01 +02:00
from __future__ import absolute_import
2020-09-26 16:19:37 +02:00
from PIL import Image as PILImage
from cStringIO import StringIO
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 = (im.size[0] * H) / 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