21 lines
614 B
Python
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()
|