LPX55 commited on
Commit
97d4592
·
verified ·
1 Parent(s): 5c4e0dc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -16
app.py CHANGED
@@ -4,7 +4,20 @@ from io import BytesIO
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,23 +38,21 @@ def split_image_grid(image, grid_cols, grid_rows):
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)
 
4
  from PIL import Image
5
  import zipfile
6
  import os
7
+ import atexit
8
+ import shutil
9
+
10
+ # Create a persistent directory to store generated files
11
+ GENERATED_FILES_DIR = "generated_files"
12
+ if not os.path.exists(GENERATED_FILES_DIR):
13
+ os.makedirs(GENERATED_FILES_DIR)
14
+
15
+ def cleanup_generated_files():
16
+ if os.path.exists(GENERATED_FILES_DIR):
17
+ shutil.rmtree(GENERATED_FILES_DIR)
18
+
19
+ # Register the cleanup function to run when the script exits
20
+ atexit.register(cleanup_generated_files)
21
 
22
  def split_image_grid(image, grid_cols, grid_rows):
23
  if isinstance(image, np.ndarray):
 
38
  return frames
39
 
40
  def zip_images(images):
41
+ zip_path = os.path.join(GENERATED_FILES_DIR, "output.zip")
42
+ with zipfile.ZipFile(zip_path, 'w') as zipf:
43
+ for idx, img in enumerate(images):
44
+ img_buffer = BytesIO()
45
+ img = Image.fromarray(img)
46
+ img.save(img_buffer, format='PNG')
47
+ img_buffer.seek(0)
48
+ zipf.writestr(f'image_{idx}.png', img_buffer.getvalue())
49
+ return zip_path
 
50
 
51
  def create_gif(images):
52
+ gif_path = os.path.join(GENERATED_FILES_DIR, "output.gif")
53
+ images_pil = [Image.fromarray(img) for img in images]
54
+ images_pil[0].save(gif_path, format='GIF', save_all=True, append_images=images_pil[1:], duration=100, loop=0)
55
+ return gif_path
 
56
 
57
  def process_image(image, grid_cols_input, grid_rows_input):
58
  frames = split_image_grid(image, grid_cols_input, grid_rows_input)