File size: 1,647 Bytes
7c18622
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47b1faf
7c18622
 
 
 
 
 
 
 
a311bab
7c18622
 
 
 
 
 
 
 
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
import gradio as gr
import numpy as np
from PIL import Image

def interpolate_images(img1, img2, num_steps):
    # Convert images to numpy arrays
    img1_array = np.array(Image.open(img1).convert('RGB'))
    img2_array = np.array(Image.open(img2).convert('RGB'))
    
    # Ensure both images have the same dimensions
    min_height = min(img1_array.shape[0], img2_array.shape[0])
    min_width = min(img1_array.shape[1], img2_array.shape[1])
    img1_array = img1_array[:min_height, :min_width]
    img2_array = img2_array[:min_height, :min_width]
    
    # Generate interpolation weights
    weights = np.linspace(0, 1, num_steps)
    
    # Perform interpolation
    interpolated_images = []
    for weight in weights:
        interpolated = img1_array * (1 - weight) + img2_array * weight
        interpolated = interpolated.astype(np.uint8)
        interpolated_images.append(Image.fromarray(interpolated))
    
    return interpolated_images

def interpolate_and_display(img1, img2, num_steps):
    interpolated_images = interpolate_images(img1, img2, num_steps+2)
    return interpolated_images

# Create the Gradio interface
iface = gr.Interface(
    fn=interpolate_and_display,
    inputs=[
        gr.Image(type="filepath", label="Image A"),
        gr.Image(type="filepath", label="Image B"),
        gr.Slider(minimum=0, maximum=100, step=1, label="Number of interpolation steps")
    ],
    outputs=gr.Gallery(label="Interpolated Images"),
    title="Image Interpolation",
    description="Upload two images and specify the number of interpolation steps to generate intermediate images."
)

# Launch the app
iface.launch(debug=True)