|
import gradio as gr |
|
import numpy as np |
|
from PIL import Image |
|
|
|
def interpolate_images(img1, img2, num_steps): |
|
|
|
img1_array = np.array(Image.open(img1).convert('RGB')) |
|
img2_array = np.array(Image.open(img2).convert('RGB')) |
|
|
|
|
|
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] |
|
|
|
|
|
weights = np.linspace(0, 1, num_steps) |
|
|
|
|
|
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 |
|
|
|
|
|
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." |
|
) |
|
|
|
|
|
iface.launch(debug=True) |