Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,8 +6,10 @@ import numpy as np
|
|
6 |
from scipy.integrate import quad_vec
|
7 |
from math import tau
|
8 |
import os
|
|
|
|
|
9 |
|
10 |
-
def fourier_transform_drawing(input_image,
|
11 |
# Convert input_image to an OpenCV image
|
12 |
input_image = np.array(input_image)
|
13 |
img = cv2.cvtColor(input_image, cv2.COLOR_RGB2BGR)
|
@@ -78,22 +80,29 @@ def fourier_transform_drawing(input_image, output_animation, frames, coefficient
|
|
78 |
time = np.linspace(0, drawing_time, num=frames)
|
79 |
anim = animation.FuncAnimation(fig, animate, frames=frames, interval=5, fargs=(coefs, time))
|
80 |
|
|
|
|
|
81 |
anim.save(output_animation, fps=15)
|
82 |
plt.close(fig)
|
83 |
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
# Gradio interface
|
87 |
interface = gr.Interface(
|
88 |
fn=fourier_transform_drawing,
|
89 |
inputs=[
|
90 |
-
|
91 |
gr.Image(label="Input Image", sources=['upload'], type="pil"),
|
92 |
-
gr.Textbox(label="Output Animation Path"),
|
93 |
gr.Slider(minimum=10, maximum=500, value=300, label="Number of Frames"),
|
94 |
gr.Slider(minimum=10, maximum=500, value=300, label="Number of Coefficients")
|
95 |
],
|
96 |
-
outputs="
|
97 |
title="Fourier Transform Drawing",
|
98 |
description="Upload an image and generate a Fourier Transform drawing animation."
|
99 |
)
|
|
|
6 |
from scipy.integrate import quad_vec
|
7 |
from math import tau
|
8 |
import os
|
9 |
+
from PIL import Image
|
10 |
+
import io
|
11 |
|
12 |
+
def fourier_transform_drawing(input_image, frames, coefficients):
|
13 |
# Convert input_image to an OpenCV image
|
14 |
input_image = np.array(input_image)
|
15 |
img = cv2.cvtColor(input_image, cv2.COLOR_RGB2BGR)
|
|
|
80 |
time = np.linspace(0, drawing_time, num=frames)
|
81 |
anim = animation.FuncAnimation(fig, animate, frames=frames, interval=5, fargs=(coefs, time))
|
82 |
|
83 |
+
output_animation = "output.mp4"
|
84 |
+
|
85 |
anim.save(output_animation, fps=15)
|
86 |
plt.close(fig)
|
87 |
|
88 |
+
# Convert the mp4 file to a GIF for display in Gradio
|
89 |
+
os.system(f"ffmpeg -i {output_animation} -vf 'fps=10,scale=320:-1:flags=lanczos' -c:v gif -loop 0 output.gif")
|
90 |
+
|
91 |
+
# Read the GIF and convert to PIL Image
|
92 |
+
with open("output.gif", 'rb') as f:
|
93 |
+
gif_image = Image.open(io.BytesIO(f.read()))
|
94 |
+
|
95 |
+
return gif_image
|
96 |
|
97 |
# Gradio interface
|
98 |
interface = gr.Interface(
|
99 |
fn=fourier_transform_drawing,
|
100 |
inputs=[
|
|
|
101 |
gr.Image(label="Input Image", sources=['upload'], type="pil"),
|
|
|
102 |
gr.Slider(minimum=10, maximum=500, value=300, label="Number of Frames"),
|
103 |
gr.Slider(minimum=10, maximum=500, value=300, label="Number of Coefficients")
|
104 |
],
|
105 |
+
outputs=gr.Image(format="gif"),
|
106 |
title="Fourier Transform Drawing",
|
107 |
description="Upload an image and generate a Fourier Transform drawing animation."
|
108 |
)
|