Add the information about the size and ratio of the read image

This commit is contained in:
xhoxye 2024-05-21 08:52:13 +08:00 committed by GitHub
parent 6308fb8b54
commit a9ef2a12c7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 44 additions and 0 deletions

View File

@ -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('<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.Column():
metadata_input_image = grh.Image(label='Drag any image generated by Fooocus here', source='upload', type='filepath')