Spaces:
Running
on
Zero
Running
on
Zero
Add error checking on grid plus spacing
Browse files- app.py +32 -26
- utils/hex_grid.py +5 -2
app.py
CHANGED
|
@@ -142,7 +142,7 @@ def end_session(req: gr.Request):
|
|
| 142 |
# Register the cleanup function
|
| 143 |
atexit.register(end_session)
|
| 144 |
|
| 145 |
-
def hex_create(hex_size, border_size, input_image_path, start_x, start_y, end_x, end_y, rotation, background_color_hex, background_opacity, border_color_hex, border_opacity, fill_hex, excluded_colors_var, filter_color, x_spacing, y_spacing, add_hex_text_option=None, custom_text_list=None, custom_text_color_list=None, sides=6):
|
| 146 |
global input_image_palette
|
| 147 |
|
| 148 |
try:
|
|
@@ -193,31 +193,37 @@ def hex_create(hex_size, border_size, input_image_path, start_x, start_y, end_x,
|
|
| 193 |
# Prepare excluded colors list
|
| 194 |
excluded_color_list = [tuple(lst) for lst in excluded_colors_var]
|
| 195 |
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
|
| 222 |
def get_model_and_lora(model_textbox):
|
| 223 |
"""
|
|
|
|
| 142 |
# Register the cleanup function
|
| 143 |
atexit.register(end_session)
|
| 144 |
|
| 145 |
+
def hex_create(hex_size, border_size, input_image_path, start_x, start_y, end_x, end_y, rotation, background_color_hex, background_opacity, border_color_hex, border_opacity, fill_hex, excluded_colors_var, filter_color, x_spacing, y_spacing, add_hex_text_option=None, custom_text_list=None, custom_text_color_list=None, sides=6, progress=gr.Progress(track_tqdm=True)):
|
| 146 |
global input_image_palette
|
| 147 |
|
| 148 |
try:
|
|
|
|
| 193 |
# Prepare excluded colors list
|
| 194 |
excluded_color_list = [tuple(lst) for lst in excluded_colors_var]
|
| 195 |
|
| 196 |
+
try:
|
| 197 |
+
# Generate the hexagon grid images
|
| 198 |
+
grid_image, overlay_image = generate_hexagon_grid_interface(
|
| 199 |
+
hex_size,
|
| 200 |
+
border_size,
|
| 201 |
+
input_image,
|
| 202 |
+
start_x,
|
| 203 |
+
start_y,
|
| 204 |
+
end_x,
|
| 205 |
+
end_y,
|
| 206 |
+
rotation,
|
| 207 |
+
background_color,
|
| 208 |
+
border_color,
|
| 209 |
+
fill_hex,
|
| 210 |
+
excluded_color_list,
|
| 211 |
+
filter_color,
|
| 212 |
+
x_spacing,
|
| 213 |
+
y_spacing,
|
| 214 |
+
add_hex_text_option,
|
| 215 |
+
custom_text_list,
|
| 216 |
+
custom_text_color_list,
|
| 217 |
+
sides
|
| 218 |
+
)
|
| 219 |
+
_,_, name, _, new_ext = get_file_parts(input_image_path)
|
| 220 |
+
grid_image_path = save_image_to_temp_png(grid_image, user_dir, "hexgrid_" + name)
|
| 221 |
+
overlay_image_path = save_image_to_temp_png(overlay_image, user_dir, "overlay_" + name)
|
| 222 |
+
return grid_image_path, overlay_image_path
|
| 223 |
+
except Exception as e:
|
| 224 |
+
print(f"Error generating hexagon grid images: {e}")
|
| 225 |
+
gr.Warning(f"Failed to generate hexagon grid images: {e}",duration=10, title="Grid Draw error")
|
| 226 |
+
return None, None
|
| 227 |
|
| 228 |
def get_model_and_lora(model_textbox):
|
| 229 |
"""
|
utils/hex_grid.py
CHANGED
|
@@ -101,7 +101,7 @@ def generate_hexagon_grid(hex_size, border_size, input_image=None, image_width=0
|
|
| 101 |
rotation_offset = -45
|
| 102 |
elif sides == 3:
|
| 103 |
# For equilateral triangles, you might offset rows by about one-third
|
| 104 |
-
# of the triangle�s height. Adjust as needed.
|
| 105 |
x_offset = hex_width + hex_horizontal_spacing
|
| 106 |
y_offset = int((hex_height * 2) + hex_vertical_spacing)
|
| 107 |
rotation_offset = -60
|
|
@@ -168,6 +168,9 @@ def generate_hexagon_grid(hex_size, border_size, input_image=None, image_width=0
|
|
| 168 |
|
| 169 |
def generate_hexagon_grid_with_text(hex_size, border_size, input_image=None, image_width=0, image_height=0, start_x=0, start_y=0, end_x=0, end_y=0, rotation=0, background_color="#ede9ac44", border_color="#12165380", fill_hex=True, excluded_color_list=excluded_color_list, filter_color=False, x_spacing=0, y_spacing=0, sides=6,
|
| 170 |
add_hex_text_option=None, custom_text_list=None, custom_text_color_list=None):
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
if input_image:
|
| 173 |
image_width, image_height = input_image.size
|
|
@@ -264,7 +267,7 @@ def generate_hexagon_grid_with_text(hex_size, border_size, input_image=None, ima
|
|
| 264 |
rotation_offset = -45
|
| 265 |
elif sides == 3:
|
| 266 |
# For equilateral triangles, you might offset rows by about one-third
|
| 267 |
-
# of the triangle�s height. Adjust as needed.
|
| 268 |
x_offset = -hex_border_size * 2 #hex_width * math.tan(math.pi / sides) - hex_border_size * 2
|
| 269 |
y_offset = -hex_border_size * 2
|
| 270 |
rotation_offset = -60
|
|
|
|
| 101 |
rotation_offset = -45
|
| 102 |
elif sides == 3:
|
| 103 |
# For equilateral triangles, you might offset rows by about one-third
|
| 104 |
+
# of the triangle�s height. Adjust as needed.
|
| 105 |
x_offset = hex_width + hex_horizontal_spacing
|
| 106 |
y_offset = int((hex_height * 2) + hex_vertical_spacing)
|
| 107 |
rotation_offset = -60
|
|
|
|
| 168 |
|
| 169 |
def generate_hexagon_grid_with_text(hex_size, border_size, input_image=None, image_width=0, image_height=0, start_x=0, start_y=0, end_x=0, end_y=0, rotation=0, background_color="#ede9ac44", border_color="#12165380", fill_hex=True, excluded_color_list=excluded_color_list, filter_color=False, x_spacing=0, y_spacing=0, sides=6,
|
| 170 |
add_hex_text_option=None, custom_text_list=None, custom_text_color_list=None):
|
| 171 |
+
if hex_size + x_spacing == 0 or hex_size + y_spacing == 0:
|
| 172 |
+
print("Hexagon size and spacing cannot equal zero")
|
| 173 |
+
raise ValueError("Hexagon size and spacing cannot equal zero")
|
| 174 |
|
| 175 |
if input_image:
|
| 176 |
image_width, image_height = input_image.size
|
|
|
|
| 267 |
rotation_offset = -45
|
| 268 |
elif sides == 3:
|
| 269 |
# For equilateral triangles, you might offset rows by about one-third
|
| 270 |
+
# of the triangle�s height. Adjust as needed.
|
| 271 |
x_offset = -hex_border_size * 2 #hex_width * math.tan(math.pi / sides) - hex_border_size * 2
|
| 272 |
y_offset = -hex_border_size * 2
|
| 273 |
rotation_offset = -60
|