bcns-gameDistributionSystem/api/modules/functions.py
2022-10-26 09:17:21 +02:00

21 lines
614 B
Python

from io import BytesIO
from PIL import Image
import base64
# Reducing the size of an artwork/image (PNG to JPEG) and base64-encode it.
# imagepath = full file image path
# expectedsize = medium or thumbnail
def reduceart(imagepath, expectedsize):
image = Image.open(imagepath)
rgb_image = image.convert("RGB")
with BytesIO() as f:
rgb_image.save(f, format="JPEG")
f.seek(0)
if expectedsize == 'thumbnail':
rgb_image.thumbnail([90,90])
rgb_image.save(f, format="JPEG")
f.seek(0)
return base64.b64encode(f.getvalue()).decode()