[picture][fix] any size for thumbnail

This commit is contained in:
Tim Gröger 2021-04-14 20:03:44 +02:00
parent 5c688df392
commit 62948cd591
2 changed files with 9 additions and 13 deletions

View File

@ -415,13 +415,9 @@ def get_drink_picture(identifier, size=None):
drink = get_drink(identifier)
if isinstance(identifier, str):
drink = Drink.query.filter(Drink.uuid == identifier).one_or_none()
try:
if drink:
return get_picture(f"{path}/{drink.uuid}", size)
except FileNotFoundError:
drink.uuid = None
db.session.commit()
raise FileNotFoundError
if drink:
return get_picture(f"{path}/{drink.uuid}", size)
raise FileNotFoundError
def delete_drink_picture(identifier):

View File

@ -1,4 +1,4 @@
import os, sys, shutil
import os, sys, shutil, io
from PIL import Image
from flask import Response
from werkzeug.exceptions import BadRequest
@ -25,6 +25,7 @@ def save_picture(picture, path):
work_image.thumbnail(thumbnail_size)
work_image.save(f"{filename}-{thumbnail_size[0]}.png", "PNG")
def get_picture(path, size=None):
try:
if size:
@ -33,9 +34,10 @@ def get_picture(path, size=None):
image = file.read()
else:
_image = Image.open(f"{path}/drink.png")
_image.thumbnail((size, size))
image = bytearray()
_image.save(bytearray, format='PNG')
_image.thumbnail((int(size), int(size)))
with io.BytesIO() as file:
_image.save(file, format="PNG")
image = file.getvalue()
else:
with open(f"{path}/drink.png", "rb") as file:
image = file.read()
@ -46,8 +48,6 @@ def get_picture(path, size=None):
raise FileNotFoundError
def delete_picture(path):
try:
shutil.rmtree(path)