Update app.py
#1
by
LPX55
- opened
app.py
CHANGED
|
@@ -3,10 +3,14 @@ import numpy as np
|
|
| 3 |
from io import BytesIO
|
| 4 |
from PIL import Image
|
| 5 |
import zipfile
|
| 6 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|
|
@@ -25,7 +29,7 @@ def split_image_grid(image, grid_cols, grid_rows):
|
|
| 25 |
return frames
|
| 26 |
|
| 27 |
def zip_images(images):
|
| 28 |
-
zip_buffer =
|
| 29 |
with zipfile.ZipFile(zip_buffer, 'w') as zipf:
|
| 30 |
for idx, img in enumerate(images):
|
| 31 |
img_buffer = BytesIO()
|
|
@@ -37,7 +41,7 @@ def zip_images(images):
|
|
| 37 |
return zip_buffer
|
| 38 |
|
| 39 |
def create_gif(images):
|
| 40 |
-
gif_buffer =
|
| 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)
|
|
@@ -55,7 +59,7 @@ def process_image_to_gif(image, grid_cols_input, grid_rows_input):
|
|
| 55 |
|
| 56 |
with gr.Blocks() as demo:
|
| 57 |
with gr.Row():
|
| 58 |
-
image_input = gr.Image(label="Input Image", type="pil")
|
| 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():
|
|
|
|
| 3 |
from io import BytesIO
|
| 4 |
from PIL import Image
|
| 5 |
import zipfile
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
class NamedBytesIO(BytesIO):
|
| 9 |
+
def __init__(self, *args, name="output.zip", **kwargs):
|
| 10 |
+
super(NamedBytesIO, self).__init__(*args, **kwargs)
|
| 11 |
+
self.name = name
|
| 12 |
|
| 13 |
def split_image_grid(image, grid_cols, grid_rows):
|
|
|
|
| 14 |
if isinstance(image, np.ndarray):
|
| 15 |
image = Image.fromarray(image)
|
| 16 |
|
|
|
|
| 29 |
return frames
|
| 30 |
|
| 31 |
def zip_images(images):
|
| 32 |
+
zip_buffer = NamedBytesIO(name="output.zip")
|
| 33 |
with zipfile.ZipFile(zip_buffer, 'w') as zipf:
|
| 34 |
for idx, img in enumerate(images):
|
| 35 |
img_buffer = BytesIO()
|
|
|
|
| 41 |
return zip_buffer
|
| 42 |
|
| 43 |
def create_gif(images):
|
| 44 |
+
gif_buffer = NamedBytesIO(name="output.gif")
|
| 45 |
images_pil = [Image.fromarray(img) for img in images]
|
| 46 |
images_pil[0].save(gif_buffer, format='GIF', save_all=True, append_images=images_pil[1:], duration=100, loop=0)
|
| 47 |
gif_buffer.seek(0)
|
|
|
|
| 59 |
|
| 60 |
with gr.Blocks() as demo:
|
| 61 |
with gr.Row():
|
| 62 |
+
image_input = gr.Image(label="Input Image", type="pil")
|
| 63 |
grid_cols_input = gr.Slider(1, 10, value=2, step=1, label="Grid Columns")
|
| 64 |
grid_rows_input = gr.Slider(1, 10, value=2, step=1, label="Grid Rows")
|
| 65 |
with gr.Row():
|