From 80dfba05f386cb5ea57040b4a0bd306861cfe964 Mon Sep 17 00:00:00 2001 From: oobabooga <112222186+oobabooga@users.noreply.github.com> Date: Tue, 4 Apr 2023 22:52:15 -0300 Subject: [PATCH] Better crop/resize cached images --- extensions/gallery/script.py | 7 ++----- modules/chat.py | 6 +++--- modules/html_generator.py | 4 ++-- server.py | 2 +- 4 files changed, 8 insertions(+), 11 deletions(-) diff --git a/extensions/gallery/script.py b/extensions/gallery/script.py index 51ab6434..f14dfd76 100644 --- a/extensions/gallery/script.py +++ b/extensions/gallery/script.py @@ -74,11 +74,8 @@ def generate_html(): path = Path(i) if path.exists(): - try: - image_html = f'' - break - except: - continue + image_html = f'' + break container_html += f'{image_html} {character}' container_html += "" diff --git a/modules/chat.py b/modules/chat.py index 2a76bddd..f7b1cc18 100644 --- a/modules/chat.py +++ b/modules/chat.py @@ -7,7 +7,7 @@ from datetime import datetime from pathlib import Path import yaml -from PIL import Image +from PIL import Image, ImageOps import modules.extensions as extensions_module import modules.shared as shared @@ -334,7 +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.thumbnail((200, 200)) + img = ImageOps.fit(img, (350, 470), Image.ANTIALIAS) 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.thumbnail((200, 200)) + img = ImageOps.fit(img, (350, 470), Image.ANTIALIAS) 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 a6b969b8..35c60b79 100644 --- a/modules/html_generator.py +++ b/modules/html_generator.py @@ -10,7 +10,7 @@ import time from pathlib import Path import markdown -from PIL import Image +from PIL import Image, ImageOps # This is to store the paths to the thumbnails of the profile pictures image_cache = {} @@ -104,7 +104,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.thumbnail((200, 200)) + img = ImageOps.fit(img, (350, 470), Image.ANTIALIAS) 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()] diff --git a/server.py b/server.py index 914448a0..f9bf3d76 100644 --- a/server.py +++ b/server.py @@ -406,7 +406,7 @@ def create_interface(): shared.gradio['character_menu'].change(chat.load_character, [shared.gradio[k] for k in ['character_menu', 'name1', 'name2']], [shared.gradio[k] for k in ['name1', 'name2', 'character_picture', 'greeting', 'context', 'display']]) shared.gradio['upload_chat_history'].upload(chat.load_history, [shared.gradio['upload_chat_history'], shared.gradio['name1'], shared.gradio['name2']], []) shared.gradio['upload_img_tavern'].upload(chat.upload_tavern_character, [shared.gradio['upload_img_tavern'], shared.gradio['name1'], shared.gradio['name2']], [shared.gradio['character_menu']]) - shared.gradio['your_picture'].change(chat.upload_your_profile_picture, shared.gradio['your_picture'], []) + shared.gradio['your_picture'].change(chat.upload_your_profile_picture, shared.gradio['your_picture'], None) reload_func = chat.redraw_html if shared.args.cai_chat else lambda : shared.history['visible'] reload_inputs = [shared.gradio['name1'], shared.gradio['name2']] if shared.args.cai_chat else []