Spaces:
Running
on
Zero
Running
on
Zero
Implement sketchpad to add layers and paths
Browse files- README.md +1 -1
- app.py +40 -21
- modules/image_utils.py +3 -3
- style_20250128.css +1 -1
README.md
CHANGED
@@ -5,7 +5,7 @@ colorFrom: yellow
|
|
5 |
colorTo: purple
|
6 |
sdk: gradio
|
7 |
python_version: 3.10.13
|
8 |
-
sdk_version: 5.20.
|
9 |
app_file: app.py
|
10 |
pinned: false
|
11 |
short_description: Transform Your Images into Mesmerizing Hexagon Grids
|
|
|
5 |
colorTo: purple
|
6 |
sdk: gradio
|
7 |
python_version: 3.10.13
|
8 |
+
sdk_version: 5.20.1
|
9 |
app_file: app.py
|
10 |
pinned: false
|
11 |
short_description: Transform Your Images into Mesmerizing Hexagon Grids
|
app.py
CHANGED
@@ -415,7 +415,7 @@ def generate_image_to_image(prompt_mash, image_input_path, image_strength, steps
|
|
415 |
return final_image
|
416 |
|
417 |
@spaces.GPU(duration=140)
|
418 |
-
def run_lora(prompt, image_input, image_strength, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale, enlarge, use_conditioned_image=False, progress=gr.Progress(track_tqdm=True)):
|
419 |
if selected_index is None:
|
420 |
raise gr.Error("You must select a LoRA before proceeding.🧨")
|
421 |
print(f"input Image: {image_input}\n")
|
@@ -429,6 +429,10 @@ def run_lora(prompt, image_input, image_strength, cfg_scale, steps, selected_ind
|
|
429 |
conditioned_image = open_image(current_prerendered_image.value).convert("RGB")
|
430 |
image_input = crop_and_resize_image(conditioned_image, width, height)
|
431 |
print(f"Conditioned Image: {image_input.size}.. converted to RGB and resized\n")
|
|
|
|
|
|
|
|
|
432 |
selected_lora = loras[selected_index]
|
433 |
lora_path = selected_lora["repo"]
|
434 |
trigger_word = selected_lora["trigger_word"]
|
@@ -593,12 +597,34 @@ def update_prompt_visibility(map_option):
|
|
593 |
gr.update(visible=is_visible),
|
594 |
gr.update(visible=is_visible)
|
595 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
596 |
|
597 |
def replace_input_with_sketch_image(sketch_image):
|
598 |
print(f"Sketch Image: {sketch_image}\n")
|
599 |
sketch, is_dict = get_image_from_dict(sketch_image)
|
600 |
return sketch
|
601 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
602 |
@spaces.GPU()
|
603 |
def getVersions():
|
604 |
return versions_html()
|
@@ -665,35 +691,23 @@ with gr.Blocks(css_paths="style_20250128.css", title=title, theme='Surn/beeuty',
|
|
665 |
image_mode="RGB",
|
666 |
format="PNG"
|
667 |
)
|
668 |
-
|
669 |
-
if image_path is None:
|
670 |
-
gr.Warning("Please upload an Input Image to get started.")
|
671 |
-
return None
|
672 |
-
img, img_path = convert_to_rgba_png(image_path)
|
673 |
-
return img_path
|
674 |
-
|
675 |
-
input_image.input(
|
676 |
-
fn=on_input_image_change,
|
677 |
-
inputs=[input_image],
|
678 |
-
outputs=[input_image], scroll_to_output=True,
|
679 |
-
)
|
680 |
with gr.Column(scale=0):
|
681 |
-
with gr.Accordion("Sketch Pad
|
682 |
with gr.Row():
|
683 |
sketch_image = gr.Sketchpad(
|
684 |
label="Sketch Image",
|
685 |
type="filepath",
|
686 |
#invert_colors=True,
|
687 |
-
#
|
688 |
#tool=['editor','select','color-sketch'],
|
689 |
placeholder="Draw a sketch or upload an image. Currently broken in gradio 5.17.1",
|
690 |
interactive=True,
|
691 |
elem_classes="centered solid imgcontainer",
|
692 |
key="imgSketch",
|
693 |
-
image_mode="
|
694 |
format="PNG",
|
695 |
-
|
696 |
-
height=512 # Default height
|
697 |
)
|
698 |
with gr.Row():
|
699 |
with gr.Column(scale=1):
|
@@ -895,10 +909,15 @@ with gr.Blocks(css_paths="style_20250128.css", title=title, theme='Surn/beeuty',
|
|
895 |
inputs=[input_image, composite_color, composite_opacity],
|
896 |
outputs=[input_image]
|
897 |
)
|
|
|
|
|
|
|
|
|
|
|
898 |
#use conditioned_image as the input_image for generate_input_image_click
|
899 |
generate_input_image_from_gallery.click(
|
900 |
fn=run_lora,
|
901 |
-
inputs=[prompt, input_image, image_strength, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale, enlarge_to_default, gr.State(True)],
|
902 |
outputs=[input_image, seed, progress_bar], scroll_to_output=True
|
903 |
)
|
904 |
prerendered_image_gallery.select(
|
@@ -908,7 +927,7 @@ with gr.Blocks(css_paths="style_20250128.css", title=title, theme='Surn/beeuty',
|
|
908 |
show_api=False, scroll_to_output=True
|
909 |
)
|
910 |
alpha_composite_slider.change(
|
911 |
-
fn=
|
912 |
inputs=[input_image, sketch_image, alpha_composite_slider],
|
913 |
outputs=[input_image],
|
914 |
scroll_to_output=True
|
@@ -941,7 +960,7 @@ with gr.Blocks(css_paths="style_20250128.css", title=title, theme='Surn/beeuty',
|
|
941 |
gr.on(
|
942 |
triggers=[generate_button.click, prompt.submit],
|
943 |
fn=run_lora,
|
944 |
-
inputs=[prompt, input_image, image_strength, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale, enlarge_to_default, gr.State(False)],
|
945 |
outputs=[input_image, seed, progress_bar]
|
946 |
)
|
947 |
|
|
|
415 |
return final_image
|
416 |
|
417 |
@spaces.GPU(duration=140)
|
418 |
+
def run_lora(prompt, map_option, image_input, image_strength, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale, enlarge, use_conditioned_image=False, progress=gr.Progress(track_tqdm=True)):
|
419 |
if selected_index is None:
|
420 |
raise gr.Error("You must select a LoRA before proceeding.🧨")
|
421 |
print(f"input Image: {image_input}\n")
|
|
|
429 |
conditioned_image = open_image(current_prerendered_image.value).convert("RGB")
|
430 |
image_input = crop_and_resize_image(conditioned_image, width, height)
|
431 |
print(f"Conditioned Image: {image_input.size}.. converted to RGB and resized\n")
|
432 |
+
if map_option != "Prompt":
|
433 |
+
prompt = PROMPTS[map_option]
|
434 |
+
# negative_prompt = NEGATIVE_PROMPTS.get(map_option, "")
|
435 |
+
|
436 |
selected_lora = loras[selected_index]
|
437 |
lora_path = selected_lora["repo"]
|
438 |
trigger_word = selected_lora["trigger_word"]
|
|
|
597 |
gr.update(visible=is_visible),
|
598 |
gr.update(visible=is_visible)
|
599 |
)
|
600 |
+
def composite_with_control_sync(input_image, sketch_image, slider_value):
|
601 |
+
# Load the images using open_image() if they are provided as file paths.
|
602 |
+
in_img = open_image(input_image) if isinstance(input_image, str) else input_image
|
603 |
+
sk_img_path, _ = get_image_from_dict(sketch_image)
|
604 |
+
sk_img = open_image(sk_img_path)
|
605 |
+
|
606 |
+
# Resize sketch image if dimensions don't match input image.
|
607 |
+
if in_img.size != sk_img.size:
|
608 |
+
sk_img = sk_img.resize(in_img.size, Image.LANCZOS)
|
609 |
+
|
610 |
+
# Now composite using the original alpha_composite_with_control function.
|
611 |
+
result_img = alpha_composite_with_control(in_img, sk_img, slider_value)
|
612 |
+
return result_img
|
613 |
|
614 |
def replace_input_with_sketch_image(sketch_image):
|
615 |
print(f"Sketch Image: {sketch_image}\n")
|
616 |
sketch, is_dict = get_image_from_dict(sketch_image)
|
617 |
return sketch
|
618 |
|
619 |
+
def on_input_image_change(image_path):
|
620 |
+
if image_path is None:
|
621 |
+
gr.Warning("Please upload an Input Image to get started.")
|
622 |
+
return None, None
|
623 |
+
img, img_path = convert_to_rgba_png(image_path)
|
624 |
+
with Image.open(img_path) as pil_img:
|
625 |
+
width, height = pil_img.size
|
626 |
+
return [img_path, gr.update(width=width, height=height)]
|
627 |
+
|
628 |
@spaces.GPU()
|
629 |
def getVersions():
|
630 |
return versions_html()
|
|
|
691 |
image_mode="RGB",
|
692 |
format="PNG"
|
693 |
)
|
694 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
695 |
with gr.Column(scale=0):
|
696 |
+
with gr.Accordion("Sketch Pad", open = False):
|
697 |
with gr.Row():
|
698 |
sketch_image = gr.Sketchpad(
|
699 |
label="Sketch Image",
|
700 |
type="filepath",
|
701 |
#invert_colors=True,
|
702 |
+
#sources=['upload','canvas'],
|
703 |
#tool=['editor','select','color-sketch'],
|
704 |
placeholder="Draw a sketch or upload an image. Currently broken in gradio 5.17.1",
|
705 |
interactive=True,
|
706 |
elem_classes="centered solid imgcontainer",
|
707 |
key="imgSketch",
|
708 |
+
image_mode="RGBA",
|
709 |
format="PNG",
|
710 |
+
brush=gr.Brush()
|
|
|
711 |
)
|
712 |
with gr.Row():
|
713 |
with gr.Column(scale=1):
|
|
|
909 |
inputs=[input_image, composite_color, composite_opacity],
|
910 |
outputs=[input_image]
|
911 |
)
|
912 |
+
input_image.input(
|
913 |
+
fn=on_input_image_change,
|
914 |
+
inputs=[input_image],
|
915 |
+
outputs=[input_image,sketch_image], scroll_to_output=True,
|
916 |
+
)
|
917 |
#use conditioned_image as the input_image for generate_input_image_click
|
918 |
generate_input_image_from_gallery.click(
|
919 |
fn=run_lora,
|
920 |
+
inputs=[prompt, map_options, input_image, image_strength, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale, enlarge_to_default, gr.State(True)],
|
921 |
outputs=[input_image, seed, progress_bar], scroll_to_output=True
|
922 |
)
|
923 |
prerendered_image_gallery.select(
|
|
|
927 |
show_api=False, scroll_to_output=True
|
928 |
)
|
929 |
alpha_composite_slider.change(
|
930 |
+
fn=composite_with_control_sync,
|
931 |
inputs=[input_image, sketch_image, alpha_composite_slider],
|
932 |
outputs=[input_image],
|
933 |
scroll_to_output=True
|
|
|
960 |
gr.on(
|
961 |
triggers=[generate_button.click, prompt.submit],
|
962 |
fn=run_lora,
|
963 |
+
inputs=[prompt, map_options, input_image, image_strength, cfg_scale, steps, selected_index, randomize_seed, seed, width, height, lora_scale, enlarge_to_default, gr.State(False)],
|
964 |
outputs=[input_image, seed, progress_bar]
|
965 |
)
|
966 |
|
modules/image_utils.py
CHANGED
@@ -17,10 +17,10 @@ from modules.color_utils import (
|
|
17 |
)
|
18 |
def get_image_from_dict(image_path):
|
19 |
if isinstance(image_path, dict) :
|
20 |
-
if '
|
21 |
-
image_path = image_path.get('image')
|
22 |
-
elif 'composite' in image_path:
|
23 |
image_path = image_path.get('composite')
|
|
|
|
|
24 |
else:
|
25 |
print("\n Unknown image dictionary.\n")
|
26 |
raise UserWarning("Unknown image dictionary.")
|
|
|
17 |
)
|
18 |
def get_image_from_dict(image_path):
|
19 |
if isinstance(image_path, dict) :
|
20 |
+
if 'composite' in image_path:
|
|
|
|
|
21 |
image_path = image_path.get('composite')
|
22 |
+
elif 'image' in image_path:
|
23 |
+
image_path = image_path.get('image')
|
24 |
else:
|
25 |
print("\n Unknown image dictionary.\n")
|
26 |
raise UserWarning("Unknown image dictionary.")
|
style_20250128.css
CHANGED
@@ -20,7 +20,7 @@
|
|
20 |
background-color: rgba(242, 218, 163, 0.62);
|
21 |
}
|
22 |
|
23 |
-
.dark .gradio-container.gradio-container-5-20-
|
24 |
background-color: rgba(41, 18, 5, 0.38) !important;
|
25 |
}
|
26 |
|
|
|
20 |
background-color: rgba(242, 218, 163, 0.62);
|
21 |
}
|
22 |
|
23 |
+
.dark .gradio-container.gradio-container-5-20-1 .contain .intro {
|
24 |
background-color: rgba(41, 18, 5, 0.38) !important;
|
25 |
}
|
26 |
|