Spaces:
Runtime error
Runtime error
create app
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
def interpolate_images(img1, img2, num_steps):
|
6 |
+
# Convert images to numpy arrays
|
7 |
+
img1_array = np.array(Image.open(img1).convert('RGB'))
|
8 |
+
img2_array = np.array(Image.open(img2).convert('RGB'))
|
9 |
+
|
10 |
+
# Ensure both images have the same dimensions
|
11 |
+
min_height = min(img1_array.shape[0], img2_array.shape[0])
|
12 |
+
min_width = min(img1_array.shape[1], img2_array.shape[1])
|
13 |
+
img1_array = img1_array[:min_height, :min_width]
|
14 |
+
img2_array = img2_array[:min_height, :min_width]
|
15 |
+
|
16 |
+
# Generate interpolation weights
|
17 |
+
weights = np.linspace(0, 1, num_steps)
|
18 |
+
|
19 |
+
# Perform interpolation
|
20 |
+
interpolated_images = []
|
21 |
+
for weight in weights:
|
22 |
+
interpolated = img1_array * (1 - weight) + img2_array * weight
|
23 |
+
interpolated = interpolated.astype(np.uint8)
|
24 |
+
interpolated_images.append(Image.fromarray(interpolated))
|
25 |
+
|
26 |
+
return interpolated_images
|
27 |
+
|
28 |
+
def interpolate_and_display(img1, img2, num_steps):
|
29 |
+
interpolated_images = interpolate_images(img1, img2, num_steps)
|
30 |
+
return interpolated_images
|
31 |
+
|
32 |
+
# Create the Gradio interface
|
33 |
+
iface = gr.Interface(
|
34 |
+
fn=interpolate_and_display,
|
35 |
+
inputs=[
|
36 |
+
gr.Image(type="filepath", label="Image A"),
|
37 |
+
gr.Image(type="filepath", label="Image B"),
|
38 |
+
gr.Slider(minimum=2, maximum=10, step=1, label="Number of interpolation steps")
|
39 |
+
],
|
40 |
+
outputs=gr.Gallery(label="Interpolated Images"),
|
41 |
+
title="Image Interpolation",
|
42 |
+
description="Upload two images and specify the number of interpolation steps to generate intermediate images."
|
43 |
+
)
|
44 |
+
|
45 |
+
# Launch the app
|
46 |
+
iface.launch(debug=True)
|