From 62948cd59178e98d52c9653a850d93ea35f9dcf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Wed, 14 Apr 2021 20:03:44 +0200 Subject: [PATCH] [picture][fix] any size for thumbnail --- .../plugins/pricelist/pricelist_controller.py | 10 +++------- flaschengeist/utils/picture.py | 12 ++++++------ 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/flaschengeist/plugins/pricelist/pricelist_controller.py b/flaschengeist/plugins/pricelist/pricelist_controller.py index fbc6965..32d2c33 100644 --- a/flaschengeist/plugins/pricelist/pricelist_controller.py +++ b/flaschengeist/plugins/pricelist/pricelist_controller.py @@ -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): diff --git a/flaschengeist/utils/picture.py b/flaschengeist/utils/picture.py index dc896f5..3f2dc40 100644 --- a/flaschengeist/utils/picture.py +++ b/flaschengeist/utils/picture.py @@ -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)