groueix commited on
Commit
29dbdec
Β·
1 Parent(s): a186d15

fixed ratio

Browse files
Files changed (1) hide show
  1. 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
- # Calculate aspect ratio
74
- ratio = w_cells / h_cells if h_cells > w_cells else w_cells / h_cells
75
- closest_ratio = None
76
- closest_ratio_str = None
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
- # Find closest ratio
88
- for ratio_str, r in ratios.items():
89
- diff = abs(ratio - r)
 
 
 
 
90
  if diff < min_diff:
91
  min_diff = diff
92
- closest_ratio = r
93
- closest_ratio_str = ratio_str
94
 
95
- example_str = ""
96
-
97
- if closest_ratio_str == "1:1":
98
- if h_cells == 2 and h_cells == 2:
99
- example_str = ' (for example, a 6" by 6" canvas)'
100
- if h_cells == 3 and h_cells == 3:
101
- example_str = ' (for example, an 8" by 8" canvas)'
102
- elif closest_ratio_str == "5:6":
103
- example_str = ' (for example, a 10" by 12" canvas)'
104
- elif closest_ratio_str == "3:4":
105
- if (h_cells == 3 and w_cells == 4) or (h_cells == 4 and w_cells == 3):
106
- example_str = ' (for example, a 6" by 8" canvas)'
107
- if (h_cells == 4 and w_cells == 6) or (h_cells == 6 and w_cells == 4):
108
- example_str = ' (for example, an 18" by 24", or a 12" by 16" canvas)'
109
- elif closest_ratio_str == "2:3" and ((h_cells == 6 and w_cells == 9) or (h_cells == 9 and w_cells == 6)):
110
- example_str = ' (for example, a 24" by 36" canvas)'
 
 
 
 
 
 
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(