Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os.path
|
2 |
+
import modules.scripts as scripts
|
3 |
+
import gradio as gr
|
4 |
+
from modules import sd_samplers, shared
|
5 |
+
from modules.processing import Processed, process_images, StableDiffusionProcessing, create_infotext
|
6 |
+
import modules.images as images
|
7 |
+
from modules.shared import opts, cmd_opts, state
|
8 |
+
from PIL import Image
|
9 |
+
import os
|
10 |
+
|
11 |
+
class Script(scripts.Script):
|
12 |
+
|
13 |
+
def title(self):
|
14 |
+
return "GIF creator from Image Slice"
|
15 |
+
|
16 |
+
def show(self, is_txt2img):
|
17 |
+
return True
|
18 |
+
|
19 |
+
def ui(self, is_txt2img):
|
20 |
+
num_cuts = gr.inputs.Slider(minimum=2, maximum=8, default=2, step=1, label="Number of cuts/slices")
|
21 |
+
gif_duration = gr.inputs.Slider(minimum=5, maximum=1000, default=150, step=5, label="GIF duration (ms)")
|
22 |
+
ping_pong_checkbox = gr.inputs.Checkbox(label="Ping-pong animation", default=True)
|
23 |
+
return [num_cuts, gif_duration, ping_pong_checkbox]
|
24 |
+
|
25 |
+
def cut_image(self, image, num_cuts):
|
26 |
+
width, height = image.size
|
27 |
+
cut_width = width // num_cuts
|
28 |
+
cut_height = height // num_cuts
|
29 |
+
parts = []
|
30 |
+
for i in range(num_cuts):
|
31 |
+
for j in range(num_cuts):
|
32 |
+
left = j * cut_width
|
33 |
+
upper = i * cut_height
|
34 |
+
right = left + cut_width
|
35 |
+
lower = upper + cut_height
|
36 |
+
parts.append(image.crop((left, upper, right, lower)))
|
37 |
+
return parts
|
38 |
+
|
39 |
+
def run(self, p, num_cuts, gif_duration, ping_pong_animation):
|
40 |
+
proc = process_images(p)
|
41 |
+
gens = proc.images
|
42 |
+
save_image_tuple = images.save_image(gens[0], p.outpath_samples, "", 0, "-Original", opts.samples_format)
|
43 |
+
save_image = save_image_tuple[0]
|
44 |
+
image = Image.open(save_image) # Cut the image into equal parts and create a GIF with optional ping-pong animation
|
45 |
+
cut_parts = self.cut_image(image, num_cuts)
|
46 |
+
gif_filename = os.path.splitext(save_image)[0] + "_gif.gif"
|
47 |
+
if ping_pong_animation:
|
48 |
+
cut_parts = cut_parts + cut_parts[-2:0:-1] # Create a ping-pong sequence
|
49 |
+
cut_parts[0].save(gif_filename, format='GIF', append_images=cut_parts[1:], save_all=True, duration=gif_duration, loop=0)
|
50 |
+
print(f"GIF created: {gif_filename}")
|
51 |
+
os.remove(save_image) # Delete the original saved image
|
52 |
+
|
53 |
+
return Processed(p, gens, 0, "")
|