LPX55 commited on
Commit
1eb15ec
·
verified ·
1 Parent(s): 38d647f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -51
app.py CHANGED
@@ -1,53 +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, "")
 
 
 
 
 
 
1
  import gradio as gr
2
+ import numpy as np
3
+ import zipfile
4
+ import io
 
5
  from PIL import Image
6
+
7
+ def split_image_grid(image, grid_size):
8
+ # Convert the image to a NumPy array
9
+ img = np.array(image)
10
+ width, height = img.shape[1], img.shape[0]
11
+ grid_width, grid_height = grid_size, grid_size
12
+
13
+ # Calculate the size of each grid cell
14
+ cell_width = width // grid_width
15
+ cell_height = height // grid_height
16
+
17
+ # Split the image into individual frames
18
+ frames = []
19
+ for i in range(grid_height):
20
+ for j in range(grid_width):
21
+ left = j * cell_width
22
+ upper = i * cell_height
23
+ right = (j + 1) * cell_width
24
+ lower = (i + 1) * cell_height
25
+ frame = img[upper:lower, left:right]
26
+ frames.append(frame)
27
+
28
+ return frames
29
+
30
+ def create_zip_file(frames):
31
+ # Create an in-memory zip file
32
+ zip_buffer = io.BytesIO()
33
+ with zipfile.ZipFile(zip_buffer, 'w') as zipf:
34
+ for idx, frame in enumerate(frames):
35
+ frame_byte_array = io.BytesIO()
36
+ # Save the frame as a PNG file in the byte array
37
+ frame_img = Image.fromarray(frame)
38
+ frame_img.save(frame_byte_array, format="PNG")
39
+ zipf.writestr(f"frame_{idx}.png", frame_byte_array.getvalue())
40
+
41
+ zip_buffer.seek(0)
42
+ return zip_buffer
43
+
44
+ def create_gif(frames):
45
+ # Create a GIF from the frames
46
+ gif_buffer = io.BytesIO()
47
+ frames_pil = [Image.fromarray(frame) for frame in frames]
48
+ frames_pil[0].save(gif_buffer, format="GIF", save_all=True, append_images=frames_pil[1:], loop=0)
49
+ gif_buffer.seek(0) # Ensure the buffer is at the beginning
50
+ return gif_buffer
51
+
52
+ def process_image(image, grid_size):
53
+ frames = split_image