fixed ratio
Browse files- copaint/gradio_ui.py +39 -37
copaint/gradio_ui.py
CHANGED
@@ -69,54 +69,56 @@ def add_grid_to_image(image, h_cells, w_cells):
|
|
69 |
return image
|
70 |
|
71 |
|
72 |
-
def get_canvas_ratio_message(h_cells, w_cells):
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
min_diff = float('inf')
|
78 |
-
|
79 |
-
# Common aspect ratios
|
80 |
-
ratios = {
|
81 |
-
"1:1": 1,
|
82 |
-
"5:6": 5/6,
|
83 |
-
"3:4": 3/4,
|
84 |
-
"2:3": 2/3
|
85 |
-
}
|
86 |
|
87 |
-
#
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
90 |
if diff < min_diff:
|
91 |
min_diff = diff
|
92 |
-
|
93 |
-
|
94 |
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
return_str = f"You have chosen a <b>{h_cells}x{w_cells} Grid ({h_cells*w_cells} squares)</b>.\n\n"
|
113 |
-
return_str += f"Preparing your canvas: <b>choose a canvas with a {closest_ratio_str} ratio</b> to respect your design's size{example_str}."
|
|
|
114 |
return f"<div style='font-size: 1.2em; line-height: 1.5;'>{return_str}</div>"
|
115 |
|
116 |
def add_grid_and_display_ratio(image, h_cells, w_cells):
|
117 |
if image is None:
|
118 |
return None, gr.update(visible=False)
|
119 |
-
return add_grid_to_image(image, h_cells, w_cells), gr.update(visible=True, value=get_canvas_ratio_message(h_cells, w_cells))
|
120 |
|
121 |
|
122 |
def process_copaint(
|
|
|
69 |
return image
|
70 |
|
71 |
|
72 |
+
def get_canvas_ratio_message(image, h_cells, w_cells):
|
73 |
+
w,h = image.size
|
74 |
+
aspect_ratio = w/h
|
75 |
+
if aspect_ratio > 1:
|
76 |
+
aspect_ratio = 1/aspect_ratio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
+
# find nearest aspect ratio in the list of predefined ones
|
79 |
+
predefined_aspect_ratios = [1/2, 2/3, 1/1, 5/6, 4/5, 5/7]
|
80 |
+
predefined_aspect_ratios_str = ["1:2", "2:3", "1:1", "5:6", "4:5", "5:7"]
|
81 |
+
min_diff = float('inf')
|
82 |
+
closest_ratio_idx = None
|
83 |
+
for idx, ratio in enumerate(predefined_aspect_ratios):
|
84 |
+
diff = abs(aspect_ratio - ratio)
|
85 |
if diff < min_diff:
|
86 |
min_diff = diff
|
87 |
+
closest_ratio_idx = idx
|
88 |
+
closest_ratio_str = predefined_aspect_ratios_str[closest_ratio_idx]
|
89 |
|
90 |
+
if min_diff > 0.2:
|
91 |
+
return None
|
92 |
+
else:
|
93 |
+
|
94 |
+
example_str = ""
|
95 |
+
|
96 |
+
if closest_ratio_str == "1:1":
|
97 |
+
if h_cells == 2 and h_cells == 2:
|
98 |
+
example_str = " for example, a 6β by 6β canvas."
|
99 |
+
if h_cells == 3 and h_cells == 3:
|
100 |
+
example_str = " for example, an 8β by 8β canvas."
|
101 |
+
elif closest_ratio_str == "5:6":
|
102 |
+
example_str = " for example, a 10β by 12β canvas."
|
103 |
+
elif closest_ratio_str == "3:4":
|
104 |
+
if (h_cells == 3 and w_cells == 4) or (h_cells == 4 and w_cells == 3):
|
105 |
+
example_str = " for example, a 6β by 8β canvas."
|
106 |
+
if (h_cells == 4 and w_cells == 6) or (h_cells == 6 and w_cells == 4):
|
107 |
+
example_str = " for example, an 18β by 24β, or a 12β by 16β canvas."
|
108 |
+
elif closest_ratio_str == "2:3" and ((h_cells == 6 and w_cells == 9) or (h_cells == 9 and w_cells == 6)):
|
109 |
+
example_str = " for example, a 24β by 36β canvas."
|
110 |
+
elif closest_ratio_str == "1:2":
|
111 |
+
example_str = " for example, a 12β by 24β canvas."
|
112 |
|
113 |
return_str = f"You have chosen a <b>{h_cells}x{w_cells} Grid ({h_cells*w_cells} squares)</b>.\n\n"
|
114 |
+
return_str += f"Preparing your canvas: <b>choose a canvas with a {closest_ratio_str} ratio</b> to respect your design's size ({w}x{h}pixels),{example_str}."
|
115 |
+
print(f"return_str: {return_str}")
|
116 |
return f"<div style='font-size: 1.2em; line-height: 1.5;'>{return_str}</div>"
|
117 |
|
118 |
def add_grid_and_display_ratio(image, h_cells, w_cells):
|
119 |
if image is None:
|
120 |
return None, gr.update(visible=False)
|
121 |
+
return add_grid_to_image(image, h_cells, w_cells), gr.update(visible=True, value=get_canvas_ratio_message(image, h_cells, w_cells))
|
122 |
|
123 |
|
124 |
def process_copaint(
|