Spaces:
Running
Running
raveblender.py
Browse files
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image, ImageOps
|
3 |
+
import numpy as np
|
4 |
+
import os
|
5 |
+
import uuid
|
6 |
+
|
7 |
+
# Ensure there's a directory for outputs
|
8 |
+
os.makedirs("outputs", exist_ok=True)
|
9 |
+
|
10 |
+
def make_square(img, size=3000, fill_color=(0, 0, 0)):
|
11 |
+
x, y = img.size
|
12 |
+
scale = size / max(x, y)
|
13 |
+
new_size = (int(x * scale), int(y * scale))
|
14 |
+
# Replace deprecated ANTIALIAS with modern equivalent
|
15 |
+
img = img.resize(new_size, Image.Resampling.LANCZOS)
|
16 |
+
new_img = Image.new("RGB", (size, size), fill_color)
|
17 |
+
new_img.paste(img, ((size - new_size[0]) // 2, (size - new_size[1]) // 2))
|
18 |
+
return new_img
|
19 |
+
|
20 |
+
def blend_images(images):
|
21 |
+
if len(images) < 2:
|
22 |
+
return "Upload at least two images.", None
|
23 |
+
|
24 |
+
try:
|
25 |
+
# Add error handling for image processing
|
26 |
+
processed = []
|
27 |
+
for img in images:
|
28 |
+
try:
|
29 |
+
processed.append(make_square(Image.open(img)))
|
30 |
+
except Exception as e:
|
31 |
+
return f"Error processing image: {str(e)}", None
|
32 |
+
|
33 |
+
base = np.array(processed[0]).astype(np.float32)
|
34 |
+
|
35 |
+
for img in processed[1:]:
|
36 |
+
base = (base + np.array(img).astype(np.float32)) / 2
|
37 |
+
|
38 |
+
final = Image.fromarray(np.uint8(base))
|
39 |
+
|
40 |
+
# Save to file
|
41 |
+
output_path = f"outputs/amalgam_{uuid.uuid4().hex[:8]}.png"
|
42 |
+
final.save(output_path)
|
43 |
+
|
44 |
+
return final, output_path
|
45 |
+
except Exception as e:
|
46 |
+
return f"Error during blending: {str(e)}", None
|
47 |
+
|
48 |
+
demo = gr.Interface(
|
49 |
+
fn=blend_images,
|
50 |
+
inputs=gr.File(file_types=["image"], file_count="multiple", label="Upload 2–5 stills"),
|
51 |
+
outputs=[
|
52 |
+
gr.Image(label="Blended Image"),
|
53 |
+
gr.File(label="Download Image")
|
54 |
+
],
|
55 |
+
title="Amalgamator",
|
56 |
+
description="Upload up to 5 stills. Outputs a 3000x3000 blended image preserving the aesthetic. Save it as PNG below."
|
57 |
+
)
|
58 |
+
|
59 |
+
demo.launch()
|