Spaces:
Running
Running
Commit
Β·
0d3f4b0
1
Parent(s):
55d1671
update: re-use output image
Browse files
app.py
CHANGED
|
@@ -68,9 +68,9 @@ def preview_padding(image, side, padding_size):
|
|
| 68 |
def outpaint_with_nano_banana(padded_image, side, custom_prompt):
|
| 69 |
"""Use Nano Banana to outpaint the padded image"""
|
| 70 |
if padded_image is None:
|
| 71 |
-
return None, "No padded image to outpaint"
|
| 72 |
if side is None:
|
| 73 |
-
return None, "No side information available"
|
| 74 |
|
| 75 |
if custom_prompt and custom_prompt.strip():
|
| 76 |
prompt = custom_prompt
|
|
@@ -94,11 +94,35 @@ def outpaint_with_nano_banana(padded_image, side, custom_prompt):
|
|
| 94 |
if getattr(part, "inline_data", None):
|
| 95 |
result_image = Image.open(io.BytesIO(part.inline_data.data))
|
| 96 |
w, h = result_image.size
|
| 97 |
-
return result_image, f"Output size: {w} x {h} pixels"
|
| 98 |
w, h = padded_image.size
|
| 99 |
-
return padded_image, f"Output size: {w} x {h} pixels (no result from API)"
|
| 100 |
except Exception as e:
|
| 101 |
-
return padded_image, f"Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
|
| 104 |
def get_image_info(image):
|
|
@@ -121,6 +145,7 @@ with gr.Blocks(title="Nano Banana Outpainting") as demo:
|
|
| 121 |
# State variables
|
| 122 |
padded_state = gr.State(None)
|
| 123 |
side_state = gr.State(None)
|
|
|
|
| 124 |
|
| 125 |
# βββββββββββββββββββββββββββββ
|
| 126 |
# ROW 1: The three image panes
|
|
@@ -182,6 +207,8 @@ with gr.Blocks(title="Nano Banana Outpainting") as demo:
|
|
| 182 |
btn_outpaint = gr.Button(
|
| 183 |
"π¨ Outpaint with Nano Banana", size="lg", variant="primary"
|
| 184 |
)
|
|
|
|
|
|
|
| 185 |
|
| 186 |
# STEP 2 PANEL (right)
|
| 187 |
with gr.Column(scale=1, min_width=360):
|
|
@@ -225,7 +252,13 @@ with gr.Blocks(title="Nano Banana Outpainting") as demo:
|
|
| 225 |
btn_outpaint.click(
|
| 226 |
fn=outpaint_with_nano_banana,
|
| 227 |
inputs=[padded_state, side_state, prompt_textbox],
|
| 228 |
-
outputs=[output_image, output_info],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
)
|
| 230 |
|
| 231 |
if __name__ == "__main__":
|
|
|
|
| 68 |
def outpaint_with_nano_banana(padded_image, side, custom_prompt):
|
| 69 |
"""Use Nano Banana to outpaint the padded image"""
|
| 70 |
if padded_image is None:
|
| 71 |
+
return None, "No padded image to outpaint", None
|
| 72 |
if side is None:
|
| 73 |
+
return None, "No side information available", None
|
| 74 |
|
| 75 |
if custom_prompt and custom_prompt.strip():
|
| 76 |
prompt = custom_prompt
|
|
|
|
| 94 |
if getattr(part, "inline_data", None):
|
| 95 |
result_image = Image.open(io.BytesIO(part.inline_data.data))
|
| 96 |
w, h = result_image.size
|
| 97 |
+
return result_image, f"Output size: {w} x {h} pixels", result_image
|
| 98 |
w, h = padded_image.size
|
| 99 |
+
return padded_image, f"Output size: {w} x {h} pixels (no result from API)", padded_image
|
| 100 |
except Exception as e:
|
| 101 |
+
return padded_image, f"Error: {str(e)}", padded_image
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
def reuse_outpainted_image(outpainted_image):
|
| 105 |
+
"""Set the most recent outpainted image as the new input and reset padding state."""
|
| 106 |
+
if outpainted_image is None:
|
| 107 |
+
return (
|
| 108 |
+
None,
|
| 109 |
+
"No outpainted image available. Run an outpaint first.",
|
| 110 |
+
None,
|
| 111 |
+
"No padding applied",
|
| 112 |
+
None,
|
| 113 |
+
None,
|
| 114 |
+
)
|
| 115 |
+
if not isinstance(outpainted_image, Image.Image):
|
| 116 |
+
outpainted_image = Image.fromarray(outpainted_image)
|
| 117 |
+
w, h = outpainted_image.size
|
| 118 |
+
return (
|
| 119 |
+
outpainted_image,
|
| 120 |
+
f"Image size: {w} x {h} pixels",
|
| 121 |
+
None,
|
| 122 |
+
"No padding applied",
|
| 123 |
+
None,
|
| 124 |
+
None,
|
| 125 |
+
)
|
| 126 |
|
| 127 |
|
| 128 |
def get_image_info(image):
|
|
|
|
| 145 |
# State variables
|
| 146 |
padded_state = gr.State(None)
|
| 147 |
side_state = gr.State(None)
|
| 148 |
+
outpainted_state = gr.State(None)
|
| 149 |
|
| 150 |
# βββββββββββββββββββββββββββββ
|
| 151 |
# ROW 1: The three image panes
|
|
|
|
| 207 |
btn_outpaint = gr.Button(
|
| 208 |
"π¨ Outpaint with Nano Banana", size="lg", variant="primary"
|
| 209 |
)
|
| 210 |
+
gr.Markdown("### Optional: Reuse the outpainted result")
|
| 211 |
+
btn_reuse = gr.Button("β»οΈ Reuse Outpainted Image")
|
| 212 |
|
| 213 |
# STEP 2 PANEL (right)
|
| 214 |
with gr.Column(scale=1, min_width=360):
|
|
|
|
| 252 |
btn_outpaint.click(
|
| 253 |
fn=outpaint_with_nano_banana,
|
| 254 |
inputs=[padded_state, side_state, prompt_textbox],
|
| 255 |
+
outputs=[output_image, output_info, outpainted_state],
|
| 256 |
+
)
|
| 257 |
+
|
| 258 |
+
btn_reuse.click(
|
| 259 |
+
fn=reuse_outpainted_image,
|
| 260 |
+
inputs=outpainted_state,
|
| 261 |
+
outputs=[input_image, image_info, padded_preview, padded_info, padded_state, side_state],
|
| 262 |
)
|
| 263 |
|
| 264 |
if __name__ == "__main__":
|