[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) drink = get_drink(identifier)
if isinstance(identifier, str): if isinstance(identifier, str):
drink = Drink.query.filter(Drink.uuid == identifier).one_or_none() drink = Drink.query.filter(Drink.uuid == identifier).one_or_none()
try: if drink:
if drink: return get_picture(f"{path}/{drink.uuid}", size)
return get_picture(f"{path}/{drink.uuid}", size) raise FileNotFoundError
except FileNotFoundError:
drink.uuid = None
db.session.commit()
raise FileNotFoundError
def delete_drink_picture(identifier): 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 PIL import Image
from flask import Response from flask import Response
from werkzeug.exceptions import BadRequest from werkzeug.exceptions import BadRequest
@ -25,6 +25,7 @@ def save_picture(picture, path):
work_image.thumbnail(thumbnail_size) work_image.thumbnail(thumbnail_size)
work_image.save(f"{filename}-{thumbnail_size[0]}.png", "PNG") work_image.save(f"{filename}-{thumbnail_size[0]}.png", "PNG")
def get_picture(path, size=None): def get_picture(path, size=None):
try: try:
if size: if size:
@ -33,9 +34,10 @@ def get_picture(path, size=None):
image = file.read() image = file.read()
else: else:
_image = Image.open(f"{path}/drink.png") _image = Image.open(f"{path}/drink.png")
_image.thumbnail((size, size)) _image.thumbnail((int(size), int(size)))
image = bytearray() with io.BytesIO() as file:
_image.save(bytearray, format='PNG') _image.save(file, format="PNG")
image = file.getvalue()
else: else:
with open(f"{path}/drink.png", "rb") as file: with open(f"{path}/drink.png", "rb") as file:
image = file.read() image = file.read()
@ -46,8 +48,6 @@ def get_picture(path, size=None):
raise FileNotFoundError raise FileNotFoundError
def delete_picture(path): def delete_picture(path):
try: try:
shutil.rmtree(path) shutil.rmtree(path)