LPX55 commited on
Commit
5c4e0dc
·
verified ·
1 Parent(s): 38dcd99

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -15
app.py CHANGED
@@ -4,6 +4,7 @@ from io import BytesIO
4
  from PIL import Image
5
  import zipfile
6
  import os
 
7
 
8
  def split_image_grid(image, grid_cols, grid_rows):
9
  if isinstance(image, np.ndarray):
@@ -24,23 +25,23 @@ def split_image_grid(image, grid_cols, grid_rows):
24
  return frames
25
 
26
  def zip_images(images):
27
- zip_buffer = BytesIO()
28
- with zipfile.ZipFile(zip_buffer, 'w') as zipf:
29
- for idx, img in enumerate(images):
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
- zip_buffer.seek(0)
36
- return {"name": "output.zip", "data": zip_buffer.getvalue()}
37
 
38
  def create_gif(images):
39
- gif_buffer = BytesIO()
40
- images_pil = [Image.fromarray(img) for img in images]
41
- images_pil[0].save(gif_buffer, format='GIF', save_all=True, append_images=images_pil[1:], duration=100, loop=0)
42
- gif_buffer.seek(0)
43
- return {"name": "output.gif", "data": gif_buffer.getvalue()}
44
 
45
  def process_image(image, grid_cols_input, grid_rows_input):
46
  frames = split_image_grid(image, grid_cols_input, grid_rows_input)
 
4
  from PIL import Image
5
  import zipfile
6
  import os
7
+ import tempfile
8
 
9
  def split_image_grid(image, grid_cols, grid_rows):
10
  if isinstance(image, np.ndarray):
 
25
  return frames
26
 
27
  def zip_images(images):
28
+ with tempfile.TemporaryDirectory() as temp_dir:
29
+ zip_path = os.path.join(temp_dir, "output.zip")
30
+ with zipfile.ZipFile(zip_path, 'w') as zipf:
31
+ for idx, img in enumerate(images):
32
+ img_buffer = BytesIO()
33
+ img = Image.fromarray(img)
34
+ img.save(img_buffer, format='PNG')
35
+ img_buffer.seek(0)
36
+ zipf.writestr(f'image_{idx}.png', img_buffer.getvalue())
37
+ return zip_path
38
 
39
  def create_gif(images):
40
+ with tempfile.TemporaryDirectory() as temp_dir:
41
+ gif_path = os.path.join(temp_dir, "output.gif")
42
+ images_pil = [Image.fromarray(img) for img in images]
43
+ images_pil[0].save(gif_path, format='GIF', save_all=True, append_images=images_pil[1:], duration=100, loop=0)
44
+ return gif_path
45
 
46
  def process_image(image, grid_cols_input, grid_rows_input):
47
  frames = split_image_grid(image, grid_cols_input, grid_rows_input)