mirror of
https://github.com/lllyasviel/Fooocus.git
synced 2026-08-16 13:13:16 +02:00
Add the information about the size and ratio of the read image
This commit is contained in:
@@ -17,6 +17,10 @@ import args_manager
|
|||||||
import copy
|
import copy
|
||||||
import launch
|
import launch
|
||||||
|
|
||||||
|
import math
|
||||||
|
import numpy as np
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
from modules.sdxl_styles import legal_style_names
|
from modules.sdxl_styles import legal_style_names
|
||||||
from modules.private_logger import get_current_html_path
|
from modules.private_logger import get_current_html_path
|
||||||
from modules.ui_gradio_extensions import reload_javascript
|
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],
|
choices=[flags.desc_type_photo, flags.desc_type_anime],
|
||||||
value=flags.desc_type_photo)
|
value=flags.desc_type_photo)
|
||||||
desc_btn = gr.Button(value='Describe this Image into Prompt')
|
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('<a href="https://github.com/lllyasviel/Fooocus/discussions/1363" target="_blank">\U0001F4D4 Document</a>')
|
gr.HTML('<a href="https://github.com/lllyasviel/Fooocus/discussions/1363" target="_blank">\U0001F4D4 Document</a>')
|
||||||
|
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.TabItem(label='Metadata') as load_tab:
|
||||||
with gr.Column():
|
with gr.Column():
|
||||||
metadata_input_image = grh.Image(label='Drag any image generated by Fooocus here', source='upload', type='filepath')
|
metadata_input_image = grh.Image(label='Drag any image generated by Fooocus here', source='upload', type='filepath')
|
||||||
|
|||||||
Reference in New Issue
Block a user