Update app.py
Browse files
app.py
CHANGED
@@ -1,53 +1,53 @@
|
|
1 |
-
import os.path
|
2 |
-
import modules.scripts as scripts
|
3 |
import gradio as gr
|
4 |
-
|
5 |
-
|
6 |
-
import
|
7 |
-
from modules.shared import opts, cmd_opts, state
|
8 |
from PIL import Image
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
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
|