Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,55 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
import rembg
|
|
|
|
| 4 |
|
| 5 |
# Function to handle image uploads, optional background removal, and collage generation
|
| 6 |
def process_images(images, remove_bg):
|
| 7 |
-
|
| 8 |
-
|
|
|
|
| 9 |
# Positioning each image onto the A4 canvas
|
| 10 |
x_offset = 0
|
| 11 |
y_offset = 0
|
| 12 |
-
for
|
| 13 |
-
img = Image.open(
|
| 14 |
-
|
| 15 |
-
# Optionally remove background
|
| 16 |
if remove_bg:
|
| 17 |
-
img = Image.open(rembg.remove(
|
| 18 |
-
|
| 19 |
-
# Resize
|
| 20 |
-
img.thumbnail((500, 500)) # Limit the size of each sticker
|
| 21 |
|
| 22 |
# Paste the image onto the canvas
|
| 23 |
-
final_collage.paste(img, (x_offset, y_offset), img)
|
| 24 |
|
| 25 |
-
# Update position for next image
|
| 26 |
y_offset += img.height
|
| 27 |
if y_offset + img.height > final_collage.height:
|
| 28 |
y_offset = 0
|
| 29 |
x_offset += img.width
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
|
| 33 |
return final_collage
|
| 34 |
|
| 35 |
# Gradio interface
|
| 36 |
with gr.Blocks() as demo:
|
| 37 |
gr.Markdown("## Sticker Collage Creator (A4)")
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
remove_bg = gr.Checkbox(label="Remove background?", value=True)
|
| 42 |
|
|
|
|
| 43 |
output = gr.Image(label="Collage", type="pil")
|
| 44 |
-
|
|
|
|
| 45 |
submit = gr.Button("Create Collage")
|
| 46 |
submit.click(process_images, inputs=[images, remove_bg], outputs=output)
|
| 47 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from PIL import Image
|
| 3 |
import rembg
|
| 4 |
+
import io
|
| 5 |
|
| 6 |
# Function to handle image uploads, optional background removal, and collage generation
|
| 7 |
def process_images(images, remove_bg):
|
| 8 |
+
# Create a blank transparent A4 canvas
|
| 9 |
+
final_collage = Image.new("RGBA", (2480, 3508), (255, 255, 255, 0)) # A4 size canvas in pixels at 300 DPI
|
| 10 |
+
|
| 11 |
# Positioning each image onto the A4 canvas
|
| 12 |
x_offset = 0
|
| 13 |
y_offset = 0
|
| 14 |
+
for image in images:
|
| 15 |
+
img = Image.open(image)
|
| 16 |
+
|
| 17 |
+
# Optionally remove the background
|
| 18 |
if remove_bg:
|
| 19 |
+
img = Image.open(io.BytesIO(rembg.remove(image.read()))) # Use rembg to remove background
|
| 20 |
+
|
| 21 |
+
# Resize image to fit within A4 dimensions
|
| 22 |
+
img.thumbnail((500, 500)) # Limit the size of each sticker to fit the canvas
|
| 23 |
|
| 24 |
# Paste the image onto the canvas
|
| 25 |
+
final_collage.paste(img, (x_offset, y_offset), img if img.mode == 'RGBA' else None)
|
| 26 |
|
| 27 |
+
# Update position for next image
|
| 28 |
y_offset += img.height
|
| 29 |
if y_offset + img.height > final_collage.height:
|
| 30 |
y_offset = 0
|
| 31 |
x_offset += img.width
|
| 32 |
+
|
| 33 |
+
# If we exceed the canvas size, we stop adding more images
|
| 34 |
+
if x_offset + img.width > final_collage.width:
|
| 35 |
+
break
|
| 36 |
|
| 37 |
return final_collage
|
| 38 |
|
| 39 |
# Gradio interface
|
| 40 |
with gr.Blocks() as demo:
|
| 41 |
gr.Markdown("## Sticker Collage Creator (A4)")
|
| 42 |
+
|
| 43 |
+
# Multiple file upload component
|
| 44 |
+
images = gr.Files(label="Upload your images", type="file", file_count="multiple", file_types=["image"])
|
| 45 |
|
| 46 |
+
# Checkbox for background removal
|
| 47 |
+
remove_bg = gr.Checkbox(label="Remove background?", value=True)
|
|
|
|
| 48 |
|
| 49 |
+
# Output image
|
| 50 |
output = gr.Image(label="Collage", type="pil")
|
| 51 |
+
|
| 52 |
+
# Button to trigger the function
|
| 53 |
submit = gr.Button("Create Collage")
|
| 54 |
submit.click(process_images, inputs=[images, remove_bg], outputs=output)
|
| 55 |
|