From cc6c7a37f35d7064341303fc10b61102c5ecae5d Mon Sep 17 00:00:00 2001 From: oobabooga <112222186+oobabooga@users.noreply.github.com> Date: Tue, 4 Apr 2023 23:03:58 -0300 Subject: [PATCH] Add make_thumbnail function --- modules/chat.py | 10 +++++----- modules/html_generator.py | 10 ++++++++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/modules/chat.py b/modules/chat.py index f7b1cc18..9af2d684 100644 --- a/modules/chat.py +++ b/modules/chat.py @@ -7,12 +7,13 @@ from datetime import datetime from pathlib import Path import yaml -from PIL import Image, ImageOps +from PIL import Image import modules.extensions as extensions_module import modules.shared as shared from modules.extensions import apply_extensions -from modules.html_generator import fix_newlines, generate_chat_html +from modules.html_generator import (fix_newlines, generate_chat_html, + make_thumbnail) from modules.text_generation import (encode, generate_reply, get_max_prompt_length) @@ -333,8 +334,7 @@ def generate_pfp_cache(character): for path in [Path(f"characters/{character}.{extension}") for extension in ['png', 'jpg', 'jpeg']]: if path.exists(): - img = Image.open(path) - img = ImageOps.fit(img, (350, 470), Image.ANTIALIAS) + img = make_thumbnail(Image.open(path)) img.save(Path('cache/pfp_character.png'), format='PNG') return img return None @@ -432,6 +432,6 @@ def upload_your_profile_picture(img): if Path("cache/pfp_me.png").exists(): Path("cache/pfp_me.png").unlink() else: - img = ImageOps.fit(img, (350, 470), Image.ANTIALIAS) + img = make_thumbnail(img) img.save(Path('cache/pfp_me.png')) print('Profile picture saved to "cache/pfp_me.png"') diff --git a/modules/html_generator.py b/modules/html_generator.py index 35c60b79..98a536f8 100644 --- a/modules/html_generator.py +++ b/modules/html_generator.py @@ -96,6 +96,13 @@ def generate_4chan_html(f): return output +def make_thumbnail(image): + image = image.resize((350, round(image.size[1]/image.size[0]*350)), Image.Resampling.LANCZOS) + if image.size[1] > 470: + image = ImageOps.fit(image, (350, 470), Image.ANTIALIAS) + + return image + def get_image_cache(path): cache_folder = Path("cache") if not cache_folder.exists(): @@ -103,8 +110,7 @@ def get_image_cache(path): mtime = os.stat(path).st_mtime if (path in image_cache and mtime != image_cache[path][0]) or (path not in image_cache): - img = Image.open(path) - img = ImageOps.fit(img, (350, 470), Image.ANTIALIAS) + img = make_thumbnail(Image.open(path)) output_file = Path(f'cache/{path.name}_cache.png') img.convert('RGB').save(output_file, format='PNG') image_cache[path] = [mtime, output_file.as_posix()]