LPX55 commited on
Commit
7d9bafe
·
verified ·
1 Parent(s): 9dff307

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -9
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
  import numpy as np
3
  import zipfile
4
- import io
5
  from PIL import Image
6
 
7
  def split_image_grid(image, grid_size):
@@ -29,10 +29,10 @@ def split_image_grid(image, grid_size):
29
 
30
  def create_zip_file(frames):
31
  # Create an in-memory zip file
32
- zip_buffer = io.BytesIO()
33
  with zipfile.ZipFile(zip_buffer, 'w') as zipf:
34
  for idx, frame in enumerate(frames):
35
- frame_byte_array = io.BytesIO()
36
  # Save the frame as a PNG file in the byte array
37
  frame_img = Image.fromarray(frame)
38
  frame_img.save(frame_byte_array, format="PNG")
@@ -43,18 +43,15 @@ def create_zip_file(frames):
43
 
44
  def create_gif(frames):
45
  # Create a GIF from the frames
46
- gif_buffer = io.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) # Ensure the buffer is at the beginning
50
  return gif_buffer
51
 
52
  def process_image(image, grid_size):
53
- # Split the image into frames
54
  frames = split_image_grid(image, grid_size)
55
- # Create a zip file from the frames
56
  zip_file = create_zip_file(frames)
57
- # Create a GIF from the frames
58
  gif_file = create_gif(frames)
59
  return zip_file, gif_file
60
 
@@ -66,7 +63,6 @@ with gr.Blocks() as demo:
66
  zip_output = gr.File(label="Download ZIP")
67
  gif_output = gr.Image(label="Generated GIF")
68
  process_button = gr.Button("Process Image")
69
-
70
  process_button.click(process_image, inputs=[image_input, grid_size_slider], outputs=[zip_output, gif_output])
71
 
72
  demo.launch(show_error=True)
 
1
  import gradio as gr
2
  import numpy as np
3
  import zipfile
4
+ from io import BytesIO
5
  from PIL import Image
6
 
7
  def split_image_grid(image, grid_size):
 
29
 
30
  def create_zip_file(frames):
31
  # Create an in-memory zip file
32
+ zip_buffer = BytesIO()
33
  with zipfile.ZipFile(zip_buffer, 'w') as zipf:
34
  for idx, frame in enumerate(frames):
35
+ frame_byte_array = BytesIO()
36
  # Save the frame as a PNG file in the byte array
37
  frame_img = Image.fromarray(frame)
38
  frame_img.save(frame_byte_array, format="PNG")
 
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
 
 
63
  zip_output = gr.File(label="Download ZIP")
64
  gif_output = gr.Image(label="Generated GIF")
65
  process_button = gr.Button("Process Image")
 
66
  process_button.click(process_image, inputs=[image_input, grid_size_slider], outputs=[zip_output, gif_output])
67
 
68
  demo.launch(show_error=True)