From a9ef2a12c79ed17290c6d959730327f58f28e606 Mon Sep 17 00:00:00 2001 From: xhoxye <129571231+xhoxye@users.noreply.github.com> Date: Tue, 21 May 2024 08:52:13 +0800 Subject: [PATCH] Add the information about the size and ratio of the read image --- webui.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/webui.py b/webui.py index 98780bff..b60a4096 100644 --- a/webui.py +++ b/webui.py @@ -17,6 +17,10 @@ import args_manager import copy import launch +import math +import numpy as np +from PIL import Image + from modules.sdxl_styles import legal_style_names from modules.private_logger import get_current_html_path from modules.ui_gradio_extensions import reload_javascript @@ -220,7 +224,47 @@ with shared.gradio_root: choices=[flags.desc_type_photo, flags.desc_type_anime], value=flags.desc_type_photo) desc_btn = gr.Button(value='Describe this Image into Prompt') + desc_image_size = gr.Markdown(label='Image Size', value='Image Size: None', elem_id='desc_image_size') gr.HTML('\U0001F4D4 Document') + def trigger_image_sizes(image): + try: + if image is not None: + image = Image.fromarray(np.uint8(image)) + width, height = image.size + ratio = round(width / height, 2) + gcd = math.gcd(width, height) + lcm_ratio = f'{width//gcd}:{height//gcd}' + size_info = f'Image Size: {width} x {height} , Ratio: {ratio} , {lcm_ratio}' + + recommended_sizes = [ + '704*1408', '704*1344', '768*1344', '768*1280', '832*1216', '832*1152', + '896*1152', '896*1088', '960*1088', '960*1024', '1024*1024', '1024*960', + '1088*960', '1088*896', '1152*896', '1152*832', '1216*832', '1280*768', + '1344*768', '1344*704', '1408*704', '1472*704', '1536*640', '1600*640', + '1664*576', '1728*576' + ] + + closest_ratio = min(recommended_sizes, key=lambda x: abs(ratio - float(x.split('*')[0]) / float(x.split('*')[1]))) + recommended_width, recommended_height = map(int, closest_ratio.split('*')) + recommended_ratio = round(recommended_width / recommended_height, 2) + recommended_gcd = math.gcd(recommended_width, recommended_height) + recommended_lcm_ratio = f'{recommended_width//recommended_gcd}:{recommended_height//recommended_gcd}' + + size_info += f'\nRecommended Size: {recommended_width} x {recommended_height} , Ratio: {recommended_ratio} , {recommended_lcm_ratio}' + + return size_info + else: + return 'Image Size: None' + except Exception as e: + return f'Error reading image: {e}' + + desc_input_image.upload( + trigger_image_sizes, + inputs=desc_input_image, + outputs=desc_image_size, + show_progress=False, + queue=False + ) with gr.TabItem(label='Metadata') as load_tab: with gr.Column(): metadata_input_image = grh.Image(label='Drag any image generated by Fooocus here', source='upload', type='filepath')