Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,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
|
12 |
|
13 |
# Calculate the size of each grid cell
|
14 |
cell_width = width // grid_width
|
@@ -20,49 +20,42 @@ def split_image_grid(image, grid_size):
|
|
20 |
for j in range(grid_width):
|
21 |
left = j * cell_width
|
22 |
upper = i * cell_height
|
23 |
-
right =
|
24 |
-
lower =
|
25 |
frame = img[upper:lower, left:right]
|
26 |
frames.append(frame)
|
27 |
|
28 |
return frames
|
29 |
|
30 |
-
def
|
31 |
-
# Create
|
32 |
zip_buffer = BytesIO()
|
33 |
with zipfile.ZipFile(zip_buffer, 'w') as zipf:
|
34 |
-
for idx,
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
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 = 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)
|
50 |
-
return gif_buffer
|
51 |
-
|
52 |
-
def process_image(image, grid_size):
|
53 |
-
frames = split_image_grid(image, grid_size)
|
54 |
-
zip_file = create_zip_file(frames)
|
55 |
-
gif_file = create_gif(frames)
|
56 |
-
return zip_file, gif_file
|
57 |
-
|
58 |
with gr.Blocks() as demo:
|
59 |
with gr.Row():
|
60 |
-
image_input = gr.Image(
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
demo.launch(show_error=True)
|
|
|
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
|
12 |
|
13 |
# Calculate the size of each grid cell
|
14 |
cell_width = width // grid_width
|
|
|
20 |
for j in range(grid_width):
|
21 |
left = j * cell_width
|
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
|
32 |
zip_buffer = BytesIO()
|
33 |
with zipfile.ZipFile(zip_buffer, 'w') as zipf:
|
34 |
+
for idx, img in enumerate(images):
|
35 |
+
# Save each image to the zip file
|
36 |
+
img_buffer = BytesIO()
|
37 |
+
img = Image.fromarray(img)
|
38 |
+
img.save(img_buffer, format='PNG')
|
39 |
+
img_buffer.seek(0)
|
40 |
+
zipf.writestr(f'image_{idx}.png', img_buffer.getvalue())
|
41 |
|
42 |
zip_buffer.seek(0)
|
43 |
return zip_buffer
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
with gr.Blocks() as demo:
|
46 |
with gr.Row():
|
47 |
+
image_input = gr.Image(label="Input Image")
|
48 |
+
grid_size_input = gr.Slider(1, 10, value=2, step=1, label="Grid Size")
|
49 |
+
split_button = gr.Button("Split Image")
|
50 |
+
zip_output = gr.File(label="Download Zipped Images")
|
51 |
+
|
52 |
+
def process_image(image, grid_size):
|
53 |
+
# Split the image into a grid of frames
|
54 |
+
frames = split_image_grid(image, (grid_size, grid_size))
|
55 |
+
# Zip the frames into a single file
|
56 |
+
zip_file = zip_images(frames)
|
57 |
+
return zip_file
|
58 |
+
|
59 |
+
split_button.click(process_image, inputs=[image_input, grid_size_input], outputs=zip_output)
|
60 |
|
61 |
demo.launch(show_error=True)
|