Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image, ImageEnhance
|
| 3 |
+
|
| 4 |
+
def composite_images(bg_image, overlay_image, x, y, scale, transparency, flip, grayscale):
|
| 5 |
+
if bg_image is None or overlay_image is None:
|
| 6 |
+
return None
|
| 7 |
+
|
| 8 |
+
bg = bg_image.convert("RGBA")
|
| 9 |
+
overlay = overlay_image.convert("RGBA")
|
| 10 |
+
|
| 11 |
+
# Apply optional effects
|
| 12 |
+
if flip:
|
| 13 |
+
overlay = overlay.transpose(Image.FLIP_LEFT_RIGHT)
|
| 14 |
+
|
| 15 |
+
if grayscale:
|
| 16 |
+
overlay = overlay.convert("L").convert("RGBA")
|
| 17 |
+
|
| 18 |
+
# Resize overlay
|
| 19 |
+
new_size = (int(overlay.width * scale), int(overlay.height * scale))
|
| 20 |
+
overlay = overlay.resize(new_size, Image.ANTIALIAS)
|
| 21 |
+
|
| 22 |
+
# Adjust transparency
|
| 23 |
+
alpha = int(255 * transparency)
|
| 24 |
+
r, g, b, a = overlay.split()
|
| 25 |
+
a = a.point(lambda p: alpha)
|
| 26 |
+
overlay = Image.merge("RGBA", (r, g, b, a))
|
| 27 |
+
|
| 28 |
+
# Create composite image
|
| 29 |
+
composed = bg.copy()
|
| 30 |
+
composed.paste(overlay, (int(x), int(y)), overlay)
|
| 31 |
+
|
| 32 |
+
return composed
|
| 33 |
+
|
| 34 |
+
with gr.Blocks() as demo:
|
| 35 |
+
gr.Markdown("### 🖼️ Overlay Image Composer with Transparency & Effects")
|
| 36 |
+
|
| 37 |
+
with gr.Row():
|
| 38 |
+
bg_input = gr.Image(label="Background Image", type="pil")
|
| 39 |
+
overlay_input = gr.Image(label="Overlay Image", type="pil")
|
| 40 |
+
|
| 41 |
+
with gr.Row():
|
| 42 |
+
x = gr.Slider(0, 1024, value=100, step=1, label="X Position")
|
| 43 |
+
y = gr.Slider(0, 1024, value=100, step=1, label="Y Position")
|
| 44 |
+
|
| 45 |
+
with gr.Row():
|
| 46 |
+
scale = gr.Slider(0.1, 3.0, value=1.0, step=0.1, label="Overlay Scale")
|
| 47 |
+
transparency = gr.Slider(0, 1, value=1.0, step=0.01, label="Overlay Transparency")
|
| 48 |
+
|
| 49 |
+
with gr.Row():
|
| 50 |
+
flip = gr.Checkbox(label="Flip Overlay Horizontally")
|
| 51 |
+
grayscale = gr.Checkbox(label="Convert Overlay to Grayscale")
|
| 52 |
+
|
| 53 |
+
output_image = gr.Image(label="Composed Image", type="pil")
|
| 54 |
+
|
| 55 |
+
run_btn = gr.Button("Generate Composite")
|
| 56 |
+
download_btn = gr.Button("Download")
|
| 57 |
+
|
| 58 |
+
# Function linking
|
| 59 |
+
run_btn.click(
|
| 60 |
+
composite_images,
|
| 61 |
+
inputs=[bg_input, overlay_input, x, y, scale, transparency, flip, grayscale],
|
| 62 |
+
outputs=output_image
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
def download_image(img):
|
| 66 |
+
if img is not None:
|
| 67 |
+
img.save("composed_output.png")
|
| 68 |
+
return "composed_output.png"
|
| 69 |
+
return None
|
| 70 |
+
|
| 71 |
+
download_btn.click(download_image, inputs=output_image, outputs=gr.File(label="Download Link"))
|
| 72 |
+
|
| 73 |
+
demo.launch()
|