File size: 1,860 Bytes
a74e93f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import gradio as gr
from PIL import Image, ImageOps
import numpy as np
import os
import uuid

# Ensure there's a directory for outputs
os.makedirs("outputs", exist_ok=True)

def make_square(img, size=3000, fill_color=(0, 0, 0)):
    x, y = img.size
    scale = size / max(x, y)
    new_size = (int(x * scale), int(y * scale))
    # Replace deprecated ANTIALIAS with modern equivalent
    img = img.resize(new_size, Image.Resampling.LANCZOS)
    new_img = Image.new("RGB", (size, size), fill_color)
    new_img.paste(img, ((size - new_size[0]) // 2, (size - new_size[1]) // 2))
    return new_img

def blend_images(images):
    if len(images) < 2:
        return "Upload at least two images.", None
    
    try:
        # Add error handling for image processing
        processed = []
        for img in images:
            try:
                processed.append(make_square(Image.open(img)))
            except Exception as e:
                return f"Error processing image: {str(e)}", None
                
        base = np.array(processed[0]).astype(np.float32)

        for img in processed[1:]:
            base = (base + np.array(img).astype(np.float32)) / 2

        final = Image.fromarray(np.uint8(base))

        # Save to file
        output_path = f"outputs/amalgam_{uuid.uuid4().hex[:8]}.png"
        final.save(output_path)

        return final, output_path
    except Exception as e:
        return f"Error during blending: {str(e)}", None

demo = gr.Interface(
    fn=blend_images,
    inputs=gr.File(file_types=["image"], file_count="multiple", label="Upload 2–5 stills"),
    outputs=[
        gr.Image(label="Blended Image"),
        gr.File(label="Download Image")
    ],
    title="Amalgamator",
    description="Upload up to 5 stills. Outputs a 3000x3000 blended image preserving the aesthetic. Save it as PNG below."
)

demo.launch()