ScoDoc/app/scodoc/imageresize.py

30 lines
744 B
Python
Raw Normal View History

2020-09-26 16:19:37 +02:00
"""Simple image resize using PIL"""
import io
2020-09-26 16:19:37 +02:00
from PIL import Image as PILImage
2021-07-11 22:56:22 +02:00
2020-09-26 16:19:37 +02:00
def ImageScale(img_file, maxx, maxy):
im = PILImage.open(img_file)
2023-08-11 15:14:16 +02:00
im.thumbnail((maxx, maxy), PILImage.LANCZOS)
out_file_str = io.BytesIO()
2020-09-26 16:19:37 +02:00
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
2021-07-09 19:50:40 +02:00
W = int((im.size[0] * H) / float(im.size[1]))
2023-08-11 15:14:16 +02:00
im.thumbnail((W, H), PILImage.LANCZOS)
out_file_str = io.BytesIO()
2020-09-26 16:19:37 +02:00
im.save(out_file_str, im.format)
out_file_str.seek(0)
tmp = out_file_str.read()
out_file_str.close()
return tmp