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 1/3] 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') From 311c4450901c69ebfc2f58391c3ceb65a4cca485 Mon Sep 17 00:00:00 2001 From: Manuel Schmid Date: Tue, 21 May 2024 22:31:27 +0200 Subject: [PATCH 2/3] feat: use available aspect ratios from config, move function to util, change default visibility of label --- modules/config.py | 2 +- modules/meta_parser.py | 2 +- modules/util.py | 22 +++++++++++++++++++ webui.py | 49 ++++++------------------------------------ 4 files changed, 31 insertions(+), 44 deletions(-) diff --git a/modules/config.py b/modules/config.py index b81e218a..d9bf2f1c 100644 --- a/modules/config.py +++ b/modules/config.py @@ -514,7 +514,7 @@ def add_ratio(x): default_aspect_ratio = add_ratio(default_aspect_ratio) -available_aspect_ratios = [add_ratio(x) for x in available_aspect_ratios] +available_aspect_ratios_labels = [add_ratio(x) for x in available_aspect_ratios] # Only write config in the first launch. diff --git a/modules/meta_parser.py b/modules/meta_parser.py index 70ab8860..1ea6ec5f 100644 --- a/modules/meta_parser.py +++ b/modules/meta_parser.py @@ -123,7 +123,7 @@ def get_resolution(key: str, fallback: str | None, source_dict: dict, results: l h = source_dict.get(key, source_dict.get(fallback, default)) width, height = eval(h) formatted = modules.config.add_ratio(f'{width}*{height}') - if formatted in modules.config.available_aspect_ratios: + if formatted in modules.config.available_aspect_ratios_labels: results.append(formatted) results.append(-1) results.append(-1) diff --git a/modules/util.py b/modules/util.py index 9e0fb294..791b3a6c 100644 --- a/modules/util.py +++ b/modules/util.py @@ -392,3 +392,25 @@ def makedirs_with_log(path): def get_enabled_loras(loras: list) -> list: return [[lora[1], lora[2]] for lora in loras if lora[0]] + + +def get_image_size_info(image: np.ndarray, available_aspect_ratios: list) -> str: + try: + 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}' + + closest_ratio = min(available_aspect_ratios, 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 + except Exception as e: + return f'Error reading image: {e}' diff --git a/webui.py b/webui.py index b60a4096..56147f43 100644 --- a/webui.py +++ b/webui.py @@ -17,10 +17,6 @@ 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 @@ -224,47 +220,16 @@ 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') + desc_image_size = gr.Markdown(label='Image Size', elem_id='desc_image_size', visible=False) 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' - ] + def trigger_show_image_properties(image): + value = modules.util.get_image_size_info(image, modules.config.available_aspect_ratios) + return gr.update(value=value, visible=True) - 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}' + desc_input_image.upload(trigger_show_image_properties, inputs=desc_input_image, + outputs=desc_image_size, show_progress=False, queue=False) - 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') @@ -309,7 +274,7 @@ with shared.gradio_root: performance_selection = gr.Radio(label='Performance', choices=flags.Performance.list(), value=modules.config.default_performance) - aspect_ratios_selection = gr.Radio(label='Aspect Ratios', choices=modules.config.available_aspect_ratios, + aspect_ratios_selection = gr.Radio(label='Aspect Ratios', choices=modules.config.available_aspect_ratios_labels, value=modules.config.default_aspect_ratio, info='width × height', elem_classes='aspect_ratios') image_number = gr.Slider(label='Image Number', minimum=1, maximum=modules.config.default_max_image_number, step=1, value=modules.config.default_image_number) From 751e867b37082519d4e4b5d1fce660e30d2dbca0 Mon Sep 17 00:00:00 2001 From: Manuel Schmid Date: Wed, 22 May 2024 20:36:49 +0200 Subject: [PATCH 3/3] refactor: extract sdxl aspect ratios to flags, use in describe as discussed in https://github.com/lllyasviel/Fooocus/pull/2971#discussion_r1608493765 https://github.com/lllyasviel/Fooocus/pull/2971#issuecomment-2123620595 --- modules/config.py | 8 +------- modules/flags.py | 7 +++++++ modules/util.py | 4 ++-- webui.py | 2 +- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/modules/config.py b/modules/config.py index d9bf2f1c..87bdd715 100644 --- a/modules/config.py +++ b/modules/config.py @@ -409,13 +409,7 @@ embeddings_downloads = get_config_item_or_set_default( ) available_aspect_ratios = get_config_item_or_set_default( key='available_aspect_ratios', - default_value=[ - '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' - ], + default_value=modules.flags.sdxl_aspect_ratios, validator=lambda x: isinstance(x, list) and all('*' in v for v in x) and len(x) > 1 ) default_aspect_ratio = get_config_item_or_set_default( diff --git a/modules/flags.py b/modules/flags.py index c9d13fd8..7119f5c5 100644 --- a/modules/flags.py +++ b/modules/flags.py @@ -78,6 +78,13 @@ inpaint_options = [inpaint_option_default, inpaint_option_detail, inpaint_option desc_type_photo = 'Photograph' desc_type_anime = 'Art/Anime' +sdxl_aspect_ratios = [ + '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' +] class MetadataScheme(Enum): FOOOCUS = 'fooocus' diff --git a/modules/util.py b/modules/util.py index 791b3a6c..b189e61f 100644 --- a/modules/util.py +++ b/modules/util.py @@ -394,7 +394,7 @@ def get_enabled_loras(loras: list) -> list: return [[lora[1], lora[2]] for lora in loras if lora[0]] -def get_image_size_info(image: np.ndarray, available_aspect_ratios: list) -> str: +def get_image_size_info(image: np.ndarray, aspect_ratios: list) -> str: try: image = Image.fromarray(np.uint8(image)) width, height = image.size @@ -403,7 +403,7 @@ def get_image_size_info(image: np.ndarray, available_aspect_ratios: list) -> str lcm_ratio = f'{width // gcd}:{height // gcd}' size_info = f'Image Size: {width} x {height}, Ratio: {ratio}, {lcm_ratio}' - closest_ratio = min(available_aspect_ratios, key=lambda x: abs(ratio - float(x.split('*')[0]) / float(x.split('*')[1]))) + closest_ratio = min(aspect_ratios, 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) diff --git a/webui.py b/webui.py index 56147f43..3cb8f8f6 100644 --- a/webui.py +++ b/webui.py @@ -224,7 +224,7 @@ with shared.gradio_root: gr.HTML('\U0001F4D4 Document') def trigger_show_image_properties(image): - value = modules.util.get_image_size_info(image, modules.config.available_aspect_ratios) + value = modules.util.get_image_size_info(image, modules.flags.sdxl_aspect_ratios) return gr.update(value=value, visible=True) desc_input_image.upload(trigger_show_image_properties, inputs=desc_input_image,