mirror of
https://github.com/altoiddealer/--sd-webui-ar-plusplus.git
synced 2026-04-28 18:21:58 +00:00
fix
return round(h * (n / d)) TypeError: unsupported operand type(s) for *: 'NoneType' and 'float'
This commit is contained in:
@@ -10,10 +10,11 @@ from math import gcd
|
||||
|
||||
aspect_ratios_dir = scripts.basedir()
|
||||
|
||||
calculator_symbol = '\U0001F5A9'
|
||||
switch_values_symbol = '\U000021C5'
|
||||
get_dimensions_symbol = '\u2B07\ufe0f'
|
||||
get_image_dimensions_symbol = '\U0001F5BC'
|
||||
calculator_symbol = "\U0001F5A9"
|
||||
switch_values_symbol = "\U000021C5"
|
||||
get_dimensions_symbol = "\u2B07\ufe0f"
|
||||
get_image_dimensions_symbol = "\U0001F5BC"
|
||||
|
||||
|
||||
class ResButton(ToolButton):
|
||||
def __init__(self, res=(512, 512), **kwargs):
|
||||
@@ -130,26 +131,30 @@ def write_resolutions_file(filename):
|
||||
|
||||
def write_js_titles_file(button_titles):
|
||||
filename = Path(aspect_ratios_dir, "javascript", "button_titles.js")
|
||||
content = ["// Do not put custom titles here. This file is overwritten each time the WebUI is started.\n"]
|
||||
content = [
|
||||
"// Do not put custom titles here. This file is overwritten each time the WebUI is started.\n"
|
||||
]
|
||||
content.append("ar_button_titles = {\n")
|
||||
counter = 0
|
||||
while counter < len(button_titles[0]):
|
||||
content.append(f" \"{button_titles[0][counter]}\" : \"{button_titles[1][counter]}\",\n")
|
||||
content.append(
|
||||
f' "{button_titles[0][counter]}" : "{button_titles[1][counter]}",\n'
|
||||
)
|
||||
counter = counter + 1
|
||||
content.append("}")
|
||||
|
||||
with open(filename, "w", encoding="utf-8") as f:
|
||||
f.writelines(content)
|
||||
|
||||
|
||||
|
||||
def get_reduced_ratio(n, d):
|
||||
n, d = list(map(int, (n, d)))
|
||||
|
||||
if n == d:
|
||||
return '1:1'
|
||||
return "1:1"
|
||||
|
||||
if n<d:
|
||||
div = gcd (d, n)
|
||||
if n < d:
|
||||
div = gcd(d, n)
|
||||
else:
|
||||
div = gcd(n, d)
|
||||
|
||||
@@ -160,16 +165,16 @@ def get_reduced_ratio(n, d):
|
||||
w = 16
|
||||
h = 10
|
||||
|
||||
return f'{w}:{h}'
|
||||
return f"{w}:{h}"
|
||||
|
||||
|
||||
def solve_aspect_ratio(w, h, n, d):
|
||||
if w != 0:
|
||||
if w != 0 and w:
|
||||
return round(w / (n / d))
|
||||
elif h != 0:
|
||||
elif h != 0 and h:
|
||||
return round(h * (n / d))
|
||||
else:
|
||||
return None
|
||||
return 0
|
||||
|
||||
|
||||
class AspectRatioScript(scripts.Script):
|
||||
@@ -209,12 +214,26 @@ class AspectRatioScript(scripts.Script):
|
||||
return scripts.AlwaysVisible
|
||||
|
||||
def ui(self, is_img2img):
|
||||
with gr.Column(elem_id=f'{"img" if is_img2img else "txt"}2img_container_aspect_ratio'):
|
||||
with gr.Column(
|
||||
elem_id=f'{"img" if is_img2img else "txt"}2img_container_aspect_ratio'
|
||||
):
|
||||
self.read_aspect_ratios()
|
||||
with gr.Row(elem_id=f'{"img" if is_img2img else "txt"}2img_row_aspect_ratio'):
|
||||
with gr.Row(
|
||||
elem_id=f'{"img" if is_img2img else "txt"}2img_row_aspect_ratio'
|
||||
):
|
||||
# Toggle calculator display button
|
||||
arc_show_calculator = gr.Button(value="Calc", visible=True, variant="secondary", elem_id="arc_show_calculator_button")
|
||||
arc_hide_calculator = gr.Button(value="Calc", visible=False, variant="primary", elem_id="arc_hide_calculator_button")
|
||||
arc_show_calculator = gr.Button(
|
||||
value="Calc",
|
||||
visible=True,
|
||||
variant="secondary",
|
||||
elem_id="arc_show_calculator_button",
|
||||
)
|
||||
arc_hide_calculator = gr.Button(
|
||||
value="Calc",
|
||||
visible=False,
|
||||
variant="primary",
|
||||
elem_id="arc_hide_calculator_button",
|
||||
)
|
||||
|
||||
# Aspect Ratio buttons
|
||||
btns = [
|
||||
@@ -236,10 +255,12 @@ class AspectRatioScript(scripts.Script):
|
||||
b.apply,
|
||||
inputs=resolution,
|
||||
outputs=resolution,
|
||||
)
|
||||
)
|
||||
|
||||
self.read_resolutions()
|
||||
with gr.Row(elem_id=f'{"img" if is_img2img else "txt"}2img_row_resolutions'):
|
||||
with gr.Row(
|
||||
elem_id=f'{"img" if is_img2img else "txt"}2img_row_resolutions'
|
||||
):
|
||||
btns = [
|
||||
ResButton(res=res, value=label)
|
||||
for res, label in zip(self.res, self.res_labels)
|
||||
@@ -268,7 +289,9 @@ class AspectRatioScript(scripts.Script):
|
||||
dummy_text4 = gr.Text(visible=False)
|
||||
|
||||
# Aspect Ratio Calculator
|
||||
with gr.Column(visible=False, variant="panel", elem_id="arc_panel") as arc_panel:
|
||||
with gr.Column(
|
||||
visible=False, variant="panel", elem_id="arc_panel"
|
||||
) as arc_panel:
|
||||
arc_title_heading = gr.Markdown(value="#### Aspect Ratio Calculator")
|
||||
with gr.Row():
|
||||
with gr.Column(min_width=150):
|
||||
@@ -281,17 +304,35 @@ class AspectRatioScript(scripts.Script):
|
||||
|
||||
with gr.Column(min_width=150):
|
||||
arc_ar_display = gr.Markdown(value="Aspect Ratio:")
|
||||
with gr.Row(elem_id=f'{"img" if is_img2img else "txt"}2img_arc_tool_buttons'):
|
||||
with gr.Row(
|
||||
elem_id=f'{"img" if is_img2img else "txt"}2img_arc_tool_buttons'
|
||||
):
|
||||
# Switch resolution values button
|
||||
arc_swap = ToolButton(value=switch_values_symbol)
|
||||
arc_swap.click(lambda w, h, w2, h2: (h, w, h2, w2), inputs=[arc_width1, arc_height1, arc_desired_width, arc_desired_height], outputs=[arc_width1, arc_height1, arc_desired_width, arc_desired_height])
|
||||
arc_swap.click(
|
||||
lambda w, h, w2, h2: (h, w, h2, w2),
|
||||
inputs=[
|
||||
arc_width1,
|
||||
arc_height1,
|
||||
arc_desired_width,
|
||||
arc_desired_height,
|
||||
],
|
||||
outputs=[
|
||||
arc_width1,
|
||||
arc_height1,
|
||||
arc_desired_width,
|
||||
arc_desired_height,
|
||||
],
|
||||
)
|
||||
|
||||
with contextlib.suppress(AttributeError):
|
||||
# For img2img tab
|
||||
if is_img2img:
|
||||
# Get slider dimensions button
|
||||
resolution = [self.i2i_w, self.i2i_h]
|
||||
arc_get_img2img_dim = ToolButton(value=get_dimensions_symbol)
|
||||
arc_get_img2img_dim = ToolButton(
|
||||
value=get_dimensions_symbol
|
||||
)
|
||||
arc_get_img2img_dim.click(
|
||||
lambda w, h: (w, h),
|
||||
inputs=resolution,
|
||||
@@ -316,24 +357,40 @@ class AspectRatioScript(scripts.Script):
|
||||
}
|
||||
|
||||
"""
|
||||
|
||||
# Get image dimensions
|
||||
def get_dims(img: list, dummy_text1, dummy_text2, dummy_text3, dummy_text4):
|
||||
def get_dims(
|
||||
img: list,
|
||||
dummy_text1,
|
||||
dummy_text2,
|
||||
dummy_text3,
|
||||
dummy_text4,
|
||||
):
|
||||
if img:
|
||||
width = img.size[0]
|
||||
height = img.size[1]
|
||||
return width, height
|
||||
else:
|
||||
return 0, 0
|
||||
|
||||
|
||||
# Get image dimensions button
|
||||
arc_get_image_dim = ToolButton(value=get_image_dimensions_symbol)
|
||||
arc_get_image_dim.click(fn=get_dims,inputs=self.image,outputs=[arc_width1, arc_height1],_js=current_tab_image)
|
||||
|
||||
arc_get_image_dim = ToolButton(
|
||||
value=get_image_dimensions_symbol
|
||||
)
|
||||
arc_get_image_dim.click(
|
||||
fn=get_dims,
|
||||
inputs=self.image,
|
||||
outputs=[arc_width1, arc_height1],
|
||||
_js=current_tab_image,
|
||||
)
|
||||
|
||||
else:
|
||||
# For txt2img tab
|
||||
# Get slider dimensions button
|
||||
resolution = [self.t2i_w, self.t2i_h]
|
||||
arc_get_txt2img_dim = ToolButton(value=get_dimensions_symbol)
|
||||
arc_get_txt2img_dim = ToolButton(
|
||||
value=get_dimensions_symbol
|
||||
)
|
||||
arc_get_txt2img_dim.click(
|
||||
lambda w, h: (w, h),
|
||||
inputs=resolution,
|
||||
@@ -341,16 +398,36 @@ class AspectRatioScript(scripts.Script):
|
||||
)
|
||||
|
||||
# Update aspect ratio display on change
|
||||
arc_width1.change(lambda w, h: (f"Aspect Ratio: **{get_reduced_ratio(w,h)}**"), inputs=[arc_width1, arc_height1], outputs=[arc_ar_display])
|
||||
arc_height1.change(lambda w, h: (f"Aspect Ratio: **{get_reduced_ratio(w,h)}**"), inputs=[arc_width1, arc_height1], outputs=[arc_ar_display])
|
||||
arc_width1.change(
|
||||
lambda w, h: (f"Aspect Ratio: **{get_reduced_ratio(w,h)}**"),
|
||||
inputs=[arc_width1, arc_height1],
|
||||
outputs=[arc_ar_display],
|
||||
)
|
||||
arc_height1.change(
|
||||
lambda w, h: (f"Aspect Ratio: **{get_reduced_ratio(w,h)}**"),
|
||||
inputs=[arc_width1, arc_height1],
|
||||
outputs=[arc_ar_display],
|
||||
)
|
||||
|
||||
with gr.Row():
|
||||
# Calculate and Apply buttons
|
||||
arc_calc_height = gr.Button(value='Calculate Height').style(full_width=False)
|
||||
arc_calc_height.click(lambda w2, w1, h1: (solve_aspect_ratio(w2,0,w1,h1)), inputs=[arc_desired_width,arc_width1,arc_height1], outputs=[arc_desired_height])
|
||||
arc_calc_width = gr.Button(value='Calculate Width').style(full_width=False)
|
||||
arc_calc_width.click(lambda h2, w1, h1: (solve_aspect_ratio(0,h2,w1,h1)), inputs=[arc_desired_height,arc_width1,arc_height1], outputs=[arc_desired_width])
|
||||
arc_apply_params = gr.Button(value='Apply')
|
||||
arc_calc_height = gr.Button(value="Calculate Height").style(
|
||||
full_width=False
|
||||
)
|
||||
arc_calc_height.click(
|
||||
lambda w2, w1, h1: (solve_aspect_ratio(w2, 0, w1, h1)),
|
||||
inputs=[arc_desired_width, arc_width1, arc_height1],
|
||||
outputs=[arc_desired_height],
|
||||
)
|
||||
arc_calc_width = gr.Button(value="Calculate Width").style(
|
||||
full_width=False
|
||||
)
|
||||
arc_calc_width.click(
|
||||
lambda h2, w1, h1: (solve_aspect_ratio(0, h2, w1, h1)),
|
||||
inputs=[arc_desired_height, arc_width1, arc_height1],
|
||||
outputs=[arc_desired_width],
|
||||
)
|
||||
arc_apply_params = gr.Button(value="Apply")
|
||||
with contextlib.suppress(AttributeError):
|
||||
if is_img2img:
|
||||
resolution = [self.i2i_w, self.i2i_h]
|
||||
@@ -365,19 +442,38 @@ class AspectRatioScript(scripts.Script):
|
||||
|
||||
# Show calculator pane (and reset number input values)
|
||||
arc_show_calculator.click(
|
||||
lambda: [gr.update(visible=True), gr.update(visible=False), gr.update(visible=True),
|
||||
gr.update(value=512), gr.update(value=512), gr.update(value=0), gr.update(value=0),
|
||||
gr.update(value="Aspect Ratio: **1:1**")],
|
||||
None,
|
||||
[arc_panel, arc_show_calculator, arc_hide_calculator,
|
||||
arc_width1, arc_height1, arc_desired_width, arc_desired_height,
|
||||
arc_ar_display]
|
||||
lambda: [
|
||||
gr.update(visible=True),
|
||||
gr.update(visible=False),
|
||||
gr.update(visible=True),
|
||||
gr.update(value=512),
|
||||
gr.update(value=512),
|
||||
gr.update(value=0),
|
||||
gr.update(value=0),
|
||||
gr.update(value="Aspect Ratio: **1:1**"),
|
||||
],
|
||||
None,
|
||||
[
|
||||
arc_panel,
|
||||
arc_show_calculator,
|
||||
arc_hide_calculator,
|
||||
arc_width1,
|
||||
arc_height1,
|
||||
arc_desired_width,
|
||||
arc_desired_height,
|
||||
arc_ar_display,
|
||||
],
|
||||
)
|
||||
# Hide calculator pane
|
||||
arc_hide_calculator.click(
|
||||
lambda: [gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)],
|
||||
None, [arc_panel, arc_show_calculator, arc_hide_calculator])
|
||||
|
||||
lambda: [
|
||||
gr.update(visible=False),
|
||||
gr.update(visible=True),
|
||||
gr.update(visible=False),
|
||||
],
|
||||
None,
|
||||
[arc_panel, arc_show_calculator, arc_hide_calculator],
|
||||
)
|
||||
|
||||
# https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/7456#issuecomment-1414465888
|
||||
def after_component(self, component, **kwargs):
|
||||
|
||||
Reference in New Issue
Block a user