Update app.py
Browse files
app.py
CHANGED
@@ -3,29 +3,23 @@ import numpy as np
|
|
3 |
import zipfile
|
4 |
from io import BytesIO
|
5 |
from PIL import Image
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
upper = i * cell_height
|
23 |
-
right = left + cell_width
|
24 |
-
lower = upper + cell_height
|
25 |
-
frame = img[upper:lower, left:right]
|
26 |
-
frames.append(frame)
|
27 |
-
|
28 |
-
return frames
|
29 |
|
30 |
def zip_images(images):
|
31 |
# Create a BytesIO object to hold the zip file
|
@@ -42,21 +36,25 @@ def zip_images(images):
|
|
42 |
zip_buffer.seek(0)
|
43 |
return zip_buffer
|
44 |
|
45 |
-
def process_image(image,
|
46 |
# Split the image into a grid of frames
|
47 |
-
|
|
|
48 |
# Zip the frames into a single zip file
|
49 |
-
zip_file = zip_images(
|
50 |
return zip_file
|
51 |
|
52 |
with gr.Blocks() as demo:
|
53 |
with gr.Row():
|
54 |
image_input = gr.Image(label="Input Image", type="filepath")
|
55 |
-
|
56 |
-
|
|
|
|
|
57 |
zip_output = gr.File(label="Output Zip File")
|
58 |
process_button = gr.Button("Process Image")
|
59 |
|
60 |
-
process_button.click(process_image, inputs=[image_input,
|
|
|
61 |
|
62 |
demo.launch(show_error=True)
|
|
|
3 |
import zipfile
|
4 |
from io import BytesIO
|
5 |
from PIL import Image
|
6 |
+
import os
|
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 |
+
return parts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
def zip_images(images):
|
25 |
# Create a BytesIO object to hold the zip file
|
|
|
36 |
zip_buffer.seek(0)
|
37 |
return zip_buffer
|
38 |
|
39 |
+
def process_image(image, num_cuts_h, num_cuts_v):
|
40 |
# Split the image into a grid of frames
|
41 |
+
cut_parts = cut_image(image, num_cuts_h, num_cuts_v)
|
42 |
+
#frames = split_image_grid(image, grid_cols_input, grid_rows_input)
|
43 |
# Zip the frames into a single zip file
|
44 |
+
zip_file = zip_images(cut_parts)
|
45 |
return zip_file
|
46 |
|
47 |
with gr.Blocks() as demo:
|
48 |
with gr.Row():
|
49 |
image_input = gr.Image(label="Input Image", type="filepath")
|
50 |
+
num_cuts_h = gr.Slider(minimum=2, maximum=8, default=3, step=1, label="Number of horizontal cuts/slices")
|
51 |
+
num_cuts_v = gr.Slider(minimum=2, maximum=8, default=3, step=1, label="Number of vertical cuts/slices")
|
52 |
+
gif_duration = gr.Slider(minimum=5, maximum=1000, default=150, step=5, label="GIF duration (ms)")
|
53 |
+
ping_pong_checkbox = gr.Checkbox(label="Ping-pong animation", default=True)
|
54 |
zip_output = gr.File(label="Output Zip File")
|
55 |
process_button = gr.Button("Process Image")
|
56 |
|
57 |
+
# process_button.click(process_image, inputs=[image_input, num_cuts_h, num_cuts_v, gif_duration, ping_pong_checkbox], outputs=zip_output)
|
58 |
+
process_button.click(process_image, inputs=[image_input, num_cuts_h, num_cuts_v], outputs=zip_output)
|
59 |
|
60 |
demo.launch(show_error=True)
|