Spaces:
Running
on
Zero
Running
on
Zero
Update Temp File naming for output and overlay images
Browse files- .gitignore +2 -1
- app.py +9 -5
- utils/file_utils.py +12 -9
- utils/image_utils.py +18 -5
.gitignore
CHANGED
@@ -167,4 +167,5 @@ cython_debug/
|
|
167 |
/temp_models
|
168 |
/.vscode/settings.json
|
169 |
**/*.pyc
|
170 |
-
**/__pycache__/
|
|
|
|
167 |
/temp_models
|
168 |
/.vscode/settings.json
|
169 |
**/*.pyc
|
170 |
+
**/__pycache__/
|
171 |
+
/utils/tmp
|
app.py
CHANGED
@@ -28,7 +28,7 @@ import logging
|
|
28 |
import gc
|
29 |
|
30 |
# Import functions from modules
|
31 |
-
from utils.file_utils import cleanup_temp_files
|
32 |
|
33 |
from utils.color_utils import (
|
34 |
hex_to_rgb,
|
@@ -193,7 +193,7 @@ def hex_create(hex_size, border_size, input_image_path, start_x, start_y, end_x,
|
|
193 |
excluded_color_list = [tuple(lst) for lst in excluded_colors_var]
|
194 |
|
195 |
# Generate the hexagon grid images
|
196 |
-
grid_image = generate_hexagon_grid_interface(
|
197 |
hex_size,
|
198 |
border_size,
|
199 |
input_image,
|
@@ -213,7 +213,10 @@ def hex_create(hex_size, border_size, input_image_path, start_x, start_y, end_x,
|
|
213 |
custom_text_list,
|
214 |
custom_text_color_list
|
215 |
)
|
216 |
-
|
|
|
|
|
|
|
217 |
|
218 |
def get_model_and_lora(model_textbox):
|
219 |
"""
|
@@ -700,10 +703,11 @@ def on_prerendered_gallery_selection(event_data: gr.SelectData):
|
|
700 |
return current_prerendered_image
|
701 |
|
702 |
def combine_images_with_lerp(input_image, output_image, alpha):
|
|
|
703 |
in_image = open_image(input_image)
|
704 |
out_image = open_image(output_image)
|
705 |
print(f"Combining images with alpha: {alpha}")
|
706 |
-
return lerp_imagemath(in_image, out_image, alpha)
|
707 |
|
708 |
def add_border(image, mask_width, mask_height, blank_color):
|
709 |
bordered_image_output = Image.open(image).convert("RGBA")
|
@@ -1385,8 +1389,8 @@ with gr.Blocks(css_paths="style_20250314.css", title=title, theme='Surn/beeuty',
|
|
1385 |
output_image = gr.Image(label="Hexagon Grid Image", image_mode = "RGBA", elem_classes="centered solid imgcontainer", format="PNG", type="filepath", key="ImgOutput",interactive=True)
|
1386 |
overlay_image = gr.Image(label="Hexagon Overlay Image", image_mode = "RGBA", elem_classes="centered solid imgcontainer", format="PNG", type="filepath", key="ImgOverlay",interactive=True)
|
1387 |
with gr.Row():
|
|
|
1388 |
output_overlay_composite = gr.Slider(0,100,50,0.5, label="Interpolate Intensity")
|
1389 |
-
output_blend_multiply_composite = gr.Slider(0,100,50,0.5, label="Overlay Intensity")
|
1390 |
output_alpha_composite = gr.Slider(0,100,50,0.5, label="Alpha Composite Intensity")
|
1391 |
with gr.Accordion("Add Margins (bleed)", open=False):
|
1392 |
with gr.Row():
|
|
|
28 |
import gc
|
29 |
|
30 |
# Import functions from modules
|
31 |
+
from utils.file_utils import cleanup_temp_files, get_file_parts
|
32 |
|
33 |
from utils.color_utils import (
|
34 |
hex_to_rgb,
|
|
|
193 |
excluded_color_list = [tuple(lst) for lst in excluded_colors_var]
|
194 |
|
195 |
# Generate the hexagon grid images
|
196 |
+
grid_image, overlay_image = generate_hexagon_grid_interface(
|
197 |
hex_size,
|
198 |
border_size,
|
199 |
input_image,
|
|
|
213 |
custom_text_list,
|
214 |
custom_text_color_list
|
215 |
)
|
216 |
+
_,_, name, _, new_ext = get_file_parts(input_image_path)
|
217 |
+
grid_image_path = save_image_to_temp_png(grid_image, user_dir, "hexgrid_" + name)
|
218 |
+
overlay_image_path = save_image_to_temp_png(overlay_image, user_dir, "overlay_" + name)
|
219 |
+
return grid_image_path, overlay_image_path
|
220 |
|
221 |
def get_model_and_lora(model_textbox):
|
222 |
"""
|
|
|
703 |
return current_prerendered_image
|
704 |
|
705 |
def combine_images_with_lerp(input_image, output_image, alpha):
|
706 |
+
directory, _, name,_, new_ext = get_file_parts(input_image)
|
707 |
in_image = open_image(input_image)
|
708 |
out_image = open_image(output_image)
|
709 |
print(f"Combining images with alpha: {alpha}")
|
710 |
+
return save_image_to_temp_png(lerp_imagemath(in_image, out_image, alpha), user_dir, f"lerp_{str(alpha)}" + name)
|
711 |
|
712 |
def add_border(image, mask_width, mask_height, blank_color):
|
713 |
bordered_image_output = Image.open(image).convert("RGBA")
|
|
|
1389 |
output_image = gr.Image(label="Hexagon Grid Image", image_mode = "RGBA", elem_classes="centered solid imgcontainer", format="PNG", type="filepath", key="ImgOutput",interactive=True)
|
1390 |
overlay_image = gr.Image(label="Hexagon Overlay Image", image_mode = "RGBA", elem_classes="centered solid imgcontainer", format="PNG", type="filepath", key="ImgOverlay",interactive=True)
|
1391 |
with gr.Row():
|
1392 |
+
output_blend_multiply_composite = gr.Slider(0,100,50,0.5, label="Multiply Intensity*")
|
1393 |
output_overlay_composite = gr.Slider(0,100,50,0.5, label="Interpolate Intensity")
|
|
|
1394 |
output_alpha_composite = gr.Slider(0,100,50,0.5, label="Alpha Composite Intensity")
|
1395 |
with gr.Accordion("Add Margins (bleed)", open=False):
|
1396 |
with gr.Row():
|
utils/file_utils.py
CHANGED
@@ -12,6 +12,17 @@ def cleanup_temp_files():
|
|
12 |
print(f"Failed to delete temp file {file_path}: {e}")
|
13 |
print("Temp files cleaned up. Goodbye")
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
def rename_file_to_lowercase_extension(file_path: str) -> str:
|
16 |
"""
|
17 |
Renames a file's extension to lowercase in place.
|
@@ -25,15 +36,7 @@ def rename_file_to_lowercase_extension(file_path: str) -> str:
|
|
25 |
Raises:
|
26 |
OSError: If there is an error renaming the file (e.g., file not found, permissions issue).
|
27 |
"""
|
28 |
-
|
29 |
-
directory, filename = os.path.split(file_path)
|
30 |
-
|
31 |
-
# Split the filename into name and extension
|
32 |
-
name, ext = os.path.splitext(filename)
|
33 |
-
|
34 |
-
# Convert the extension to lowercase
|
35 |
-
new_ext = ext.lower()
|
36 |
-
|
37 |
# If the extension changes, rename the file
|
38 |
if ext != new_ext:
|
39 |
new_filename = name + new_ext
|
|
|
12 |
print(f"Failed to delete temp file {file_path}: {e}")
|
13 |
print("Temp files cleaned up. Goodbye")
|
14 |
|
15 |
+
def get_file_parts(file_path: str):
|
16 |
+
# Split the path into directory and filename
|
17 |
+
directory, filename = os.path.split(file_path)
|
18 |
+
|
19 |
+
# Split the filename into name and extension
|
20 |
+
name, ext = os.path.splitext(filename)
|
21 |
+
|
22 |
+
# Convert the extension to lowercase
|
23 |
+
new_ext = ext.lower()
|
24 |
+
return directory, filename, name, ext, new_ext
|
25 |
+
|
26 |
def rename_file_to_lowercase_extension(file_path: str) -> str:
|
27 |
"""
|
28 |
Renames a file's extension to lowercase in place.
|
|
|
36 |
Raises:
|
37 |
OSError: If there is an error renaming the file (e.g., file not found, permissions issue).
|
38 |
"""
|
39 |
+
directory, filename, name, ext, new_ext = get_file_parts(file_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
# If the extension changes, rename the file
|
41 |
if ext != new_ext:
|
42 |
new_filename = name + new_ext
|
utils/image_utils.py
CHANGED
@@ -15,7 +15,7 @@ from utils.color_utils import (
|
|
15 |
detect_color_format,
|
16 |
update_color_opacity
|
17 |
)
|
18 |
-
from utils.file_utils import rename_file_to_lowercase_extension
|
19 |
|
20 |
|
21 |
|
@@ -45,9 +45,9 @@ def save_image_to_temp_png(image_source, user_dir: str = None, file_name: str =
|
|
45 |
user_dir = tempfile.gettempdir()
|
46 |
|
47 |
if file_name is None:
|
48 |
-
file_name =
|
49 |
|
50 |
-
temp_filepath = os.path.join(user_dir, file_name.lower())
|
51 |
os.makedirs(user_dir, exist_ok=True)
|
52 |
|
53 |
# Save the image as PNG
|
@@ -415,8 +415,11 @@ def multiply_and_blend_images(base_image, image2, alpha_percent=50):
|
|
415 |
Returns:
|
416 |
PIL.Image.Image: The blended image.
|
417 |
"""
|
|
|
|
|
418 |
alpha = alpha_percent / 100.0
|
419 |
if isinstance(base_image, str):
|
|
|
420 |
base_image = open_image(base_image)
|
421 |
if isinstance(image2, str):
|
422 |
image2 = open_image(image2)
|
@@ -430,7 +433,10 @@ def multiply_and_blend_images(base_image, image2, alpha_percent=50):
|
|
430 |
|
431 |
# Blend the multiplied result with the original
|
432 |
blended_image = Image.blend(base_image, multiplied_image, alpha)
|
433 |
-
|
|
|
|
|
|
|
434 |
return blended_image
|
435 |
|
436 |
def alpha_composite_with_control(base_image, image_with_alpha, alpha_percent=100):
|
@@ -445,9 +451,12 @@ def alpha_composite_with_control(base_image, image_with_alpha, alpha_percent=100
|
|
445 |
Returns:
|
446 |
PIL.Image.Image: The resulting image after alpha compositing.
|
447 |
"""
|
|
|
|
|
448 |
image_with_alpha, isdict = get_image_from_dict(image_with_alpha)
|
449 |
alpha_multiplier = alpha_percent / 100.0
|
450 |
if isinstance(base_image, str):
|
|
|
451 |
base_image = open_image(base_image)
|
452 |
if isinstance(image_with_alpha, str):
|
453 |
image_with_alpha = open_image(image_with_alpha)
|
@@ -465,7 +474,11 @@ def alpha_composite_with_control(base_image, image_with_alpha, alpha_percent=100
|
|
465 |
|
466 |
# Composite the images
|
467 |
result = Image.alpha_composite(base_image, image_with_alpha)
|
468 |
-
|
|
|
|
|
|
|
|
|
469 |
return result
|
470 |
|
471 |
def apply_alpha_mask(image, mask_image, invert = False):
|
|
|
15 |
detect_color_format,
|
16 |
update_color_opacity
|
17 |
)
|
18 |
+
from utils.file_utils import rename_file_to_lowercase_extension, get_file_parts
|
19 |
|
20 |
|
21 |
|
|
|
45 |
user_dir = tempfile.gettempdir()
|
46 |
|
47 |
if file_name is None:
|
48 |
+
file_name = "{uuid.uuid4()}"
|
49 |
|
50 |
+
temp_filepath = os.path.join(user_dir, file_name.lower() + ".png")
|
51 |
os.makedirs(user_dir, exist_ok=True)
|
52 |
|
53 |
# Save the image as PNG
|
|
|
415 |
Returns:
|
416 |
PIL.Image.Image: The blended image.
|
417 |
"""
|
418 |
+
name = None
|
419 |
+
directory = None
|
420 |
alpha = alpha_percent / 100.0
|
421 |
if isinstance(base_image, str):
|
422 |
+
directory, _, name,_,_ = get_file_parts(base_image)
|
423 |
base_image = open_image(base_image)
|
424 |
if isinstance(image2, str):
|
425 |
image2 = open_image(image2)
|
|
|
433 |
|
434 |
# Blend the multiplied result with the original
|
435 |
blended_image = Image.blend(base_image, multiplied_image, alpha)
|
436 |
+
if name is not None:
|
437 |
+
new_image_path = os.path.join(directory, name + f"_mb{str(alpha_percent)}.png")
|
438 |
+
blended_image.save(new_image_path)
|
439 |
+
return new_image_path
|
440 |
return blended_image
|
441 |
|
442 |
def alpha_composite_with_control(base_image, image_with_alpha, alpha_percent=100):
|
|
|
451 |
Returns:
|
452 |
PIL.Image.Image: The resulting image after alpha compositing.
|
453 |
"""
|
454 |
+
name = None
|
455 |
+
directory = None
|
456 |
image_with_alpha, isdict = get_image_from_dict(image_with_alpha)
|
457 |
alpha_multiplier = alpha_percent / 100.0
|
458 |
if isinstance(base_image, str):
|
459 |
+
directory, _, name,_, new_ext = get_file_parts(base_image)
|
460 |
base_image = open_image(base_image)
|
461 |
if isinstance(image_with_alpha, str):
|
462 |
image_with_alpha = open_image(image_with_alpha)
|
|
|
474 |
|
475 |
# Composite the images
|
476 |
result = Image.alpha_composite(base_image, image_with_alpha)
|
477 |
+
if name is not None:
|
478 |
+
new_image_path = os.path.join(directory, name + f"_alpha{str(alpha_percent)}.png")
|
479 |
+
result.save(new_image_path)
|
480 |
+
return new_image_path
|
481 |
+
return blended_image
|
482 |
return result
|
483 |
|
484 |
def apply_alpha_mask(image, mask_image, invert = False):
|