Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image
|
3 |
import io
|
|
|
|
|
4 |
|
5 |
-
def create_gif(editor1_output, editor2_output):
|
6 |
img1 = editor1_output["composite"].convert('RGBA')
|
7 |
img2 = editor2_output["composite"].convert('RGBA')
|
8 |
|
@@ -14,21 +16,29 @@ def create_gif(editor1_output, editor2_output):
|
|
14 |
img1 = img1.resize(size, Image.LANCZOS)
|
15 |
img2 = img2.resize(size, Image.LANCZOS)
|
16 |
|
17 |
-
|
18 |
-
|
|
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
# Save as GIF
|
34 |
output = io.BytesIO()
|
@@ -44,7 +54,7 @@ def create_gif(editor1_output, editor2_output):
|
|
44 |
|
45 |
# Gradio interface
|
46 |
with gr.Blocks() as iface:
|
47 |
-
gr.Markdown("#
|
48 |
|
49 |
with gr.Row():
|
50 |
with gr.Column(scale=2):
|
@@ -52,7 +62,7 @@ with gr.Blocks() as iface:
|
|
52 |
label="Edit Image 1",
|
53 |
brush=gr.Brush(colors=["#ff0000", "#00ff00", "#0000ff"]),
|
54 |
eraser=gr.Eraser(default_size=10),
|
55 |
-
height=
|
56 |
width="100%",
|
57 |
crop_size="1:1",
|
58 |
layers=True,
|
@@ -63,7 +73,7 @@ with gr.Blocks() as iface:
|
|
63 |
label="Edit Image 2",
|
64 |
brush=gr.Brush(colors=["#ff0000", "#00ff00", "#0000ff"]),
|
65 |
eraser=gr.Eraser(default_size=10),
|
66 |
-
height=
|
67 |
width="100%",
|
68 |
crop_size="1:1",
|
69 |
layers=True,
|
@@ -71,6 +81,7 @@ with gr.Blocks() as iface:
|
|
71 |
)
|
72 |
|
73 |
with gr.Row():
|
|
|
74 |
generate_button = gr.Button("Generate GIF")
|
75 |
|
76 |
with gr.Row():
|
@@ -78,7 +89,7 @@ with gr.Blocks() as iface:
|
|
78 |
|
79 |
generate_button.click(
|
80 |
create_gif,
|
81 |
-
inputs=[image_editor1, image_editor2],
|
82 |
outputs=[output_gif]
|
83 |
)
|
84 |
|
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image
|
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')
|
9 |
img2 = editor2_output["composite"].convert('RGBA')
|
10 |
|
|
|
16 |
img1 = img1.resize(size, Image.LANCZOS)
|
17 |
img2 = img2.resize(size, Image.LANCZOS)
|
18 |
|
19 |
+
if transition_type == "slide":
|
20 |
+
full_width = size[0]
|
21 |
+
step = full_width // (total_frames // 2)
|
22 |
|
23 |
+
# Create sliding transition
|
24 |
+
for i in range(0, full_width, step):
|
25 |
+
frame = Image.new('RGBA', size)
|
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 |
+
frames.append(frame.convert('P', palette=Image.ADAPTIVE))
|
29 |
|
30 |
+
for i in range(full_width, 0, -step):
|
31 |
+
frame = Image.new('RGBA', size)
|
32 |
+
frame.paste(img2, (0, 0))
|
33 |
+
frame.paste(img1.crop((0, 0, i, size[1])), (full_width - i, 0), mask=img1.crop((0, 0, i, size[1])))
|
34 |
+
frames.append(frame.convert('P', palette=Image.ADAPTIVE))
|
35 |
+
else: # rotate transition
|
36 |
+
for angle in range(0, 360, 360 // total_frames):
|
37 |
+
mask = Image.new('L', size, 0)
|
38 |
+
draw = Image.Draw(mask)
|
39 |
+
draw.pieslice([0, 0, size[0], size[1]], -90, angle - 90, fill=255)
|
40 |
+
frame = Image.composite(img2, img1, mask)
|
41 |
+
frames.append(frame.convert('P', palette=Image.ADAPTIVE))
|
42 |
|
43 |
# Save as GIF
|
44 |
output = io.BytesIO()
|
|
|
54 |
|
55 |
# Gradio interface
|
56 |
with gr.Blocks() as iface:
|
57 |
+
gr.Markdown("# 2GIF Transition Slider")
|
58 |
|
59 |
with gr.Row():
|
60 |
with gr.Column(scale=2):
|
|
|
62 |
label="Edit Image 1",
|
63 |
brush=gr.Brush(colors=["#ff0000", "#00ff00", "#0000ff"]),
|
64 |
eraser=gr.Eraser(default_size=10),
|
65 |
+
height=400,
|
66 |
width="100%",
|
67 |
crop_size="1:1",
|
68 |
layers=True,
|
|
|
73 |
label="Edit Image 2",
|
74 |
brush=gr.Brush(colors=["#ff0000", "#00ff00", "#0000ff"]),
|
75 |
eraser=gr.Eraser(default_size=10),
|
76 |
+
height=400,
|
77 |
width="100%",
|
78 |
crop_size="1:1",
|
79 |
layers=True,
|
|
|
81 |
)
|
82 |
|
83 |
with gr.Row():
|
84 |
+
transition_type = gr.Radio(["slide", "rotate"], label="Transition Type", value="slide")
|
85 |
generate_button = gr.Button("Generate GIF")
|
86 |
|
87 |
with gr.Row():
|
|
|
89 |
|
90 |
generate_button.click(
|
91 |
create_gif,
|
92 |
+
inputs=[image_editor1, image_editor2, transition_type],
|
93 |
outputs=[output_gif]
|
94 |
)
|
95 |
|