Update app.py
Browse files
app.py
CHANGED
@@ -1,60 +1,70 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
-
import zipfile
|
4 |
from io import BytesIO
|
5 |
from PIL import Image
|
6 |
-
import
|
7 |
-
|
8 |
-
|
9 |
-
def cut_image(image, num_cuts_h, num_cuts_v):
|
10 |
-
width, height = image.size
|
11 |
-
cut_width = width // num_cuts_h
|
12 |
-
cut_height = height // num_cuts_v
|
13 |
-
parts = []
|
14 |
-
for i in range(num_cuts_v):
|
15 |
-
for j in range(num_cuts_h):
|
16 |
-
left = j * cut_width
|
17 |
-
upper = i * cut_height
|
18 |
-
right = left + cut_width
|
19 |
-
lower = upper + cut_height
|
20 |
-
parts.append(image.crop((left, upper, right, lower)))
|
21 |
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
def zip_images(images):
|
25 |
-
# Create a BytesIO object to hold the zip file
|
26 |
zip_buffer = BytesIO()
|
27 |
with zipfile.ZipFile(zip_buffer, 'w') as zipf:
|
28 |
for idx, img in enumerate(images):
|
29 |
-
# Save each image to the zip file
|
30 |
img_buffer = BytesIO()
|
31 |
-
img = Image.fromarray(img)
|
32 |
img.save(img_buffer, format='PNG')
|
33 |
img_buffer.seek(0)
|
34 |
zipf.writestr(f'image_{idx}.png', img_buffer.getvalue())
|
35 |
-
|
36 |
zip_buffer.seek(0)
|
37 |
return zip_buffer
|
38 |
|
39 |
-
def
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
45 |
return zip_file
|
46 |
|
|
|
|
|
|
|
|
|
|
|
47 |
with gr.Blocks() as demo:
|
48 |
with gr.Row():
|
49 |
-
image_input = gr.Image(label="Input Image", type="
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
59 |
|
60 |
demo.launch(show_error=True)
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
|
|
3 |
from io import BytesIO
|
4 |
from PIL import Image
|
5 |
+
import zipfile
|
6 |
+
import cv2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
def split_image_grid(image, grid_cols, grid_rows):
|
9 |
+
# Ensure image is a PIL Image
|
10 |
+
if isinstance(image, np.ndarray):
|
11 |
+
image = Image.fromarray(image)
|
12 |
+
|
13 |
+
width, height = image.width, image.height
|
14 |
+
cell_width = width // grid_cols
|
15 |
+
cell_height = height // grid_rows
|
16 |
+
frames = []
|
17 |
+
for i in range(grid_rows):
|
18 |
+
for j in range(grid_cols):
|
19 |
+
left = j * cell_width
|
20 |
+
upper = i * cell_height
|
21 |
+
right = left + cell_width
|
22 |
+
lower = upper + cell_height
|
23 |
+
frame = image.crop((left, upper, right, lower))
|
24 |
+
frames.append(np.array(frame))
|
25 |
+
return frames
|
26 |
|
27 |
def zip_images(images):
|
|
|
28 |
zip_buffer = BytesIO()
|
29 |
with zipfile.ZipFile(zip_buffer, 'w') as zipf:
|
30 |
for idx, img in enumerate(images):
|
|
|
31 |
img_buffer = BytesIO()
|
32 |
+
img = Image.fromarray(img)
|
33 |
img.save(img_buffer, format='PNG')
|
34 |
img_buffer.seek(0)
|
35 |
zipf.writestr(f'image_{idx}.png', img_buffer.getvalue())
|
|
|
36 |
zip_buffer.seek(0)
|
37 |
return zip_buffer
|
38 |
|
39 |
+
def create_gif(images):
|
40 |
+
gif_buffer = BytesIO()
|
41 |
+
images_pil = [Image.fromarray(img) for img in images]
|
42 |
+
images_pil[0].save(gif_buffer, format='GIF', save_all=True, append_images=images_pil[1:], duration=100, loop=0)
|
43 |
+
gif_buffer.seek(0)
|
44 |
+
return gif_buffer
|
45 |
+
|
46 |
+
def process_image(image, grid_cols_input, grid_rows_input):
|
47 |
+
frames = split_image_grid(image, grid_cols_input, grid_rows_input)
|
48 |
+
zip_file = zip_images(frames)
|
49 |
return zip_file
|
50 |
|
51 |
+
def process_image_to_gif(image, grid_cols_input, grid_rows_input):
|
52 |
+
frames = split_image_grid(image, grid_cols_input, grid_rows_input)
|
53 |
+
gif_file = create_gif(frames)
|
54 |
+
return gif_file
|
55 |
+
|
56 |
with gr.Blocks() as demo:
|
57 |
with gr.Row():
|
58 |
+
image_input = gr.Image(label="Input Image", type="pil") # Changed to "pil" for easier handling
|
59 |
+
grid_cols_input = gr.Slider(1, 10, value=2, step=1, label="Grid Columns")
|
60 |
+
grid_rows_input = gr.Slider(1, 10, value=2, step=1, label="Grid Rows")
|
61 |
+
with gr.Row():
|
62 |
+
zip_button = gr.Button("Create Zip File")
|
63 |
+
gif_button = gr.Button("Create GIF")
|
64 |
+
with gr.Row():
|
65 |
+
zip_output = gr.File(label="Download Zip File")
|
66 |
+
gif_output = gr.File(label="Download GIF")
|
67 |
+
zip_button.click(process_image, inputs=[image_input, grid_cols_input, grid_rows_input], outputs=zip_output)
|
68 |
+
gif_button.click(process_image_to_gif, inputs=[image_input, grid_cols_input, grid_rows_input], outputs=gif_output)
|
69 |
|
70 |
demo.launch(show_error=True)
|