Skip to content
This repository was archived by the owner on Sep 26, 2024. It is now read-only.

feat: Add Send to Txt2Img & Img2Img buttons #25

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions javascript/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,21 @@ function depth_removeBackground() {
depth_lib_canvas.setBackgroundImage(0, depth_lib_canvas.renderAll.bind(depth_lib_canvas));
}

function depth_sendImage() {
function depth_sendImageTxt2Img() {
depth_sendImage(
'#txt2img_controlnet',
switch_to_txt2img,
);
}

function depth_sendImageImg2Img() {
depth_sendImage(
'#img2img_controlnet',
switch_to_img2img,
);
}

function depth_sendImage(controlNetDivId, switchFn) {
if (depth_lib_canvas.backgroundImage) depth_lib_canvas.backgroundImage.opacity = 0;
depth_lib_canvas.discardActiveObject();
depth_lib_canvas.renderAll();
Expand All @@ -127,9 +141,9 @@ function depth_sendImage() {
dt.items.add(file);
const list = dt.files;

const divControlNet = depth_gradioApp().querySelector("#txt2img_controlnet");
const divControlNet = depth_gradioApp().querySelector(controlNetDivId);
if (divControlNet) {
switch_to_txt2img();
switchFn();

// open the ControlNet accordion if it's not already open
// but give up if it takes longer than 5 secs
Expand All @@ -139,10 +153,10 @@ function depth_sendImage() {
let waitUntilHasClassOpenCount = 0;
const waitUntilHasClassOpen = async () => {
waitUntilHasClassOpenCount++;
if (waitUntilHasClassOpenCount > 50) {
return false;
} else if (labelControlNet.classList.contains("open")) {
if (labelControlNet.classList.contains("open")) {
return true;
} else if (waitUntilHasClassOpenCount > 50) {
return false;
} else {
setTimeout(() => waitUntilHasClassOpen(), 100)
}
Expand All @@ -158,9 +172,10 @@ function depth_sendImage() {
const event = new Event('change', { 'bubbles': true, "composed": true });
input.dispatchEvent(event);
}

if (depth_lib_canvas.backgroundImage) depth_lib_canvas.backgroundImage.opacity = 0.5
depth_lib_canvas.renderAll()
});
if (depth_lib_canvas.backgroundImage) depth_lib_canvas.backgroundImage.opacity = 0.5
depth_lib_canvas.renderAll()
}

function depth_setBrightness(br) {
Expand Down
7 changes: 4 additions & 3 deletions scripts/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ def on_ui_tabs():
opacity = gr.Slider(label="Opacity", minimum=0.01, maximum=1, value=1, step=0.01, interactive=True)

with gr.Column():
# gradioooooo...
canvas = gr.HTML('<canvas id="depth_lib_canvas" width="512" height="512" style="margin: 0.25rem; border-radius: 0.25rem; border: 0.5px solid"></canvas><style>#examples .gr-sample-image {background-color: #e5e7eb}</style>')
with gr.Row():
png_output = gr.Button(value="Save PNG")
send_output = gr.Button(value="Send to ControlNet")
send_output_txt2img = gr.Button(value="Send to Txt2Img")
send_output_img2img = gr.Button(value="Send to Img2Img")


width.change(None, [width, height], None, _js="(w, h) => {depth_resizeCanvas(w, h)}")
Expand All @@ -66,7 +66,8 @@ def on_ui_tabs():
bg_remove.click(None, [], None, _js="depth_removeBackground")
add.click(None, [png_input_area], None, _js="(path) => {depth_addImg(path)}")
remove.click(None, [], None, _js="depth_removeSelection")
send_output.click(None, [], None, _js="depth_sendImage")
send_output_txt2img.click(None, [], None, _js="() => depth_sendImageTxt2Img()")
send_output_img2img.click(None, [], None, _js="() => depth_sendImageImg2Img()")
reset_btn.click(None, [], None, _js="depth_resetCanvas")

return [(depth_lib, "Depth Library", "depth_lib")]
Expand Down