sbt-idp/cope2n-api/fwd_api/utils/image.py

15 lines
474 B
Python
Raw Normal View History

2023-12-12 05:54:34 +00:00
from PIL import Image
def resize(image, max_w=2048, max_h=2048):
cur_w, cur_h = image.width, image.height
image_bytes = image.samples
image = Image.frombytes("RGB", [cur_w, cur_h], image_bytes)
if cur_h > max_w or cur_h > max_h:
ratio_w = max_w/cur_w
ratio_h = max_h/cur_h
ratio = min([ratio_h, ratio_w])
new_w = int(ratio*cur_w)
new_h = int(ratio*cur_h)
image = image.resize((new_w, new_h))
return image