Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,6 +3,11 @@ from PIL import Image, ImageDraw
|
|
3 |
import io
|
4 |
import numpy as np
|
5 |
from math import cos, radians, sin
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
def create_gif(editor1_output, editor2_output, transition_type):
|
8 |
img1 = editor1_output["composite"].convert('RGBA')
|
@@ -12,7 +17,7 @@ def create_gif(editor1_output, editor2_output, transition_type):
|
|
12 |
duration = 100 # Duration for each frame in milliseconds
|
13 |
total_frames = 18 # Total number of frames
|
14 |
|
15 |
-
size = (
|
16 |
img1 = img1.resize(size, Image.LANCZOS)
|
17 |
img2 = img2.resize(size, Image.LANCZOS)
|
18 |
|
@@ -26,7 +31,7 @@ def create_gif(editor1_output, editor2_output, transition_type):
|
|
26 |
frame.paste(img1, (0, 0))
|
27 |
frame.paste(img2.crop((0, 0, i, size[1])), (full_width - i, 0), mask=img2.crop((0, 0, i, size[1])))
|
28 |
draw = ImageDraw.Draw(frame)
|
29 |
-
draw.line((full_width - i, 0, full_width - i, size[1]), fill=(0, 255, 0), width=
|
30 |
frames.append(frame.convert('P', palette=Image.ADAPTIVE))
|
31 |
|
32 |
for i in range(full_width, 0, -step):
|
@@ -34,7 +39,7 @@ def create_gif(editor1_output, editor2_output, transition_type):
|
|
34 |
frame.paste(img2, (0, 0))
|
35 |
frame.paste(img1.crop((0, 0, i, size[1])), (full_width - i, 0), mask=img1.crop((0, 0, i, size[1])))
|
36 |
draw = ImageDraw.Draw(frame)
|
37 |
-
draw.line((full_width - i, 0, full_width - i, size[1]), fill=(0, 255, 0), width=
|
38 |
frames.append(frame.convert('P', palette=Image.ADAPTIVE))
|
39 |
else: # rotate transition
|
40 |
mask_size = (size[0] * 2, size[1] * 2)
|
@@ -55,8 +60,8 @@ def create_gif(editor1_output, editor2_output, transition_type):
|
|
55 |
end_y1 = center_y + int(size[1] * 1.5 * sin(radians(reverse_angle)))
|
56 |
end_x2 = center_x - int(size[0] * 1.5 * cos(radians(reverse_angle)))
|
57 |
end_y2 = center_y - int(size[1] * 1.5 * sin(radians(reverse_angle)))
|
58 |
-
draw.line([center_x, center_y, end_x1, end_y1], fill=(0, 255, 0), width=
|
59 |
-
draw.line([center_x, center_y, end_x2, end_y2], fill=(0, 255, 0), width=
|
60 |
|
61 |
frames.append(frame.convert('P', palette=Image.ADAPTIVE))
|
62 |
|
@@ -65,12 +70,19 @@ def create_gif(editor1_output, editor2_output, transition_type):
|
|
65 |
frames[0].save(output, format='GIF', save_all=True, append_images=frames[1:], duration=duration, loop=0, optimize=True)
|
66 |
output.seek(0)
|
67 |
|
68 |
-
# Save the GIF to a
|
69 |
-
|
70 |
-
|
|
|
|
|
71 |
f.write(output.getvalue())
|
72 |
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
# Gradio interface
|
76 |
with gr.Blocks() as iface:
|
@@ -105,12 +117,20 @@ with gr.Blocks() as iface:
|
|
105 |
generate_button = gr.Button("Generate GIF")
|
106 |
|
107 |
with gr.Row():
|
108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
generate_button.click(
|
111 |
create_gif,
|
112 |
inputs=[image_editor1, image_editor2, transition_type],
|
113 |
-
outputs=[
|
114 |
)
|
115 |
|
116 |
# Launch the interface
|
|
|
3 |
import io
|
4 |
import numpy as np
|
5 |
from math import cos, radians, sin
|
6 |
+
import os
|
7 |
+
import time
|
8 |
+
|
9 |
+
# Global variable to store generated GIFs
|
10 |
+
generated_gifs = []
|
11 |
|
12 |
def create_gif(editor1_output, editor2_output, transition_type):
|
13 |
img1 = editor1_output["composite"].convert('RGBA')
|
|
|
17 |
duration = 100 # Duration for each frame in milliseconds
|
18 |
total_frames = 18 # Total number of frames
|
19 |
|
20 |
+
size = (512, 512) # Increased size for better quality
|
21 |
img1 = img1.resize(size, Image.LANCZOS)
|
22 |
img2 = img2.resize(size, Image.LANCZOS)
|
23 |
|
|
|
31 |
frame.paste(img1, (0, 0))
|
32 |
frame.paste(img2.crop((0, 0, i, size[1])), (full_width - i, 0), mask=img2.crop((0, 0, i, size[1])))
|
33 |
draw = ImageDraw.Draw(frame)
|
34 |
+
draw.line((full_width - i, 0, full_width - i, size[1]), fill=(0, 255, 0), width=4)
|
35 |
frames.append(frame.convert('P', palette=Image.ADAPTIVE))
|
36 |
|
37 |
for i in range(full_width, 0, -step):
|
|
|
39 |
frame.paste(img2, (0, 0))
|
40 |
frame.paste(img1.crop((0, 0, i, size[1])), (full_width - i, 0), mask=img1.crop((0, 0, i, size[1])))
|
41 |
draw = ImageDraw.Draw(frame)
|
42 |
+
draw.line((full_width - i, 0, full_width - i, size[1]), fill=(0, 255, 0), width=4)
|
43 |
frames.append(frame.convert('P', palette=Image.ADAPTIVE))
|
44 |
else: # rotate transition
|
45 |
mask_size = (size[0] * 2, size[1] * 2)
|
|
|
60 |
end_y1 = center_y + int(size[1] * 1.5 * sin(radians(reverse_angle)))
|
61 |
end_x2 = center_x - int(size[0] * 1.5 * cos(radians(reverse_angle)))
|
62 |
end_y2 = center_y - int(size[1] * 1.5 * sin(radians(reverse_angle)))
|
63 |
+
draw.line([center_x, center_y, end_x1, end_y1], fill=(0, 255, 0), width=6)
|
64 |
+
draw.line([center_x, center_y, end_x2, end_y2], fill=(0, 255, 0), width=6)
|
65 |
|
66 |
frames.append(frame.convert('P', palette=Image.ADAPTIVE))
|
67 |
|
|
|
70 |
frames[0].save(output, format='GIF', save_all=True, append_images=frames[1:], duration=duration, loop=0, optimize=True)
|
71 |
output.seek(0)
|
72 |
|
73 |
+
# Save the GIF to a file with a unique name
|
74 |
+
os.makedirs("outputs", exist_ok=True)
|
75 |
+
timestamp = int(time.time())
|
76 |
+
output_path = f"outputs/output_{timestamp}.gif"
|
77 |
+
with open(output_path, "wb") as f:
|
78 |
f.write(output.getvalue())
|
79 |
|
80 |
+
# Add the new GIF to the list of generated GIFs
|
81 |
+
global generated_gifs
|
82 |
+
generated_gifs.append((output_path, f"{transition_type.capitalize()} transition"))
|
83 |
+
|
84 |
+
# Return the updated list of generated GIFs
|
85 |
+
return generated_gifs
|
86 |
|
87 |
# Gradio interface
|
88 |
with gr.Blocks() as iface:
|
|
|
117 |
generate_button = gr.Button("Generate GIF")
|
118 |
|
119 |
with gr.Row():
|
120 |
+
output_gallery = gr.Gallery(
|
121 |
+
label="Generated GIFs",
|
122 |
+
show_label=True,
|
123 |
+
elem_id="output_gallery",
|
124 |
+
columns=3,
|
125 |
+
rows=2,
|
126 |
+
height=400,
|
127 |
+
object_fit="contain"
|
128 |
+
)
|
129 |
|
130 |
generate_button.click(
|
131 |
create_gif,
|
132 |
inputs=[image_editor1, image_editor2, transition_type],
|
133 |
+
outputs=[output_gallery]
|
134 |
)
|
135 |
|
136 |
# Launch the interface
|