LPX55 commited on
Commit
7a68495
·
verified ·
1 Parent(s): b33ec08

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -10
app.py CHANGED
@@ -40,19 +40,19 @@ def split_image_grid(image, grid_cols, grid_rows):
40
  frames.append(np.array(frame))
41
  return frames
42
 
43
- def interpolate_frames(frames, factor=2):
44
  interpolated_frames = []
45
  for i in range(len(frames) - 1):
46
  frame1 = frames[i]
47
  frame2 = frames[i + 1]
48
  interpolated_frames.append(frame1)
49
- for j in range(1, factor):
50
- t = j / factor
51
  frame_t = cv2.addWeighted(frame1, 1 - t, frame2, t, 0)
52
  interpolated_frames.append(frame_t)
53
  interpolated_frames.append(frames[-1])
54
  return interpolated_frames
55
-
56
  def enhance_gif(images):
57
  enhanced_images = []
58
  for img in images:
@@ -61,23 +61,25 @@ def enhance_gif(images):
61
  enhanced_images.append(np.array(img))
62
  return enhanced_images
63
 
64
- def create_gif_imageio(images, fps=10, loop=0):
65
  duration = 1000 / fps # Convert FPS to milliseconds
 
 
66
  gif_path = os.path.join(GENERATED_FILES_DIR, "output_enhanced.gif")
67
  images_pil = [Image.fromarray(img) for img in images]
68
  imageio.mimsave(gif_path, images_pil, duration=duration, loop=loop)
69
  return gif_path
70
-
71
  def process_image(image, grid_cols_input, grid_rows_input):
72
  frames = split_image_grid(image, grid_cols_input, grid_rows_input)
73
  zip_file = zip_images(frames)
74
  return zip_file
75
 
76
- def process_image_to_gif(image, grid_cols_input, grid_rows_input, fps_input):
77
  frames = split_image_grid(image, grid_cols_input, grid_rows_input)
78
- interpolated_frames = interpolate_frames(frames, factor=2)
79
  enhanced_frames = enhance_gif(interpolated_frames)
80
- gif_file = create_gif_imageio(enhanced_frames, fps=fps_input, loop=0)
81
 
82
  # Preview the first frame of the GIF
83
  # preview_image = Image.fromarray(enhanced_frames[0])
@@ -103,6 +105,9 @@ with gr.Blocks() as demo:
103
  grid_cols_input = gr.Slider(1, 10, value=2, step=1, label="Grid Columns")
104
  grid_rows_input = gr.Slider(1, 10, value=2, step=1, label="Grid Rows")
105
  fps_input = gr.Slider(1, 30, value=10, step=1, label="FPS (Frames per Second)")
 
 
 
106
  with gr.Row():
107
  zip_button = gr.Button("Create Zip File")
108
  gif_button = gr.Button("Create GIF")
@@ -113,7 +118,7 @@ with gr.Blocks() as demo:
113
  zip_button.click(process_image, inputs=[image_input, grid_cols_input, grid_rows_input], outputs=zip_output)
114
  gif_button.click(
115
  process_image_to_gif,
116
- inputs=[image_input, grid_cols_input, grid_rows_input, fps_input],
117
  outputs=[preview_image, gif_output]
118
  )
119
 
 
40
  frames.append(np.array(frame))
41
  return frames
42
 
43
+ def interpolate_frames(frames, interpolation_factor=2):
44
  interpolated_frames = []
45
  for i in range(len(frames) - 1):
46
  frame1 = frames[i]
47
  frame2 = frames[i + 1]
48
  interpolated_frames.append(frame1)
49
+ for j in range(1, interpolation_factor):
50
+ t = j / interpolation_factor
51
  frame_t = cv2.addWeighted(frame1, 1 - t, frame2, t, 0)
52
  interpolated_frames.append(frame_t)
53
  interpolated_frames.append(frames[-1])
54
  return interpolated_frames
55
+
56
  def enhance_gif(images):
57
  enhanced_images = []
58
  for img in images:
 
61
  enhanced_images.append(np.array(img))
62
  return enhanced_images
63
 
64
+ def create_gif_imageio(images, fps=10, ping_pong_animation=False, loop=0):
65
  duration = 1000 / fps # Convert FPS to milliseconds
66
+ if ping_pong_animation:
67
+ images = images + images[-2:0:-1] # Create a ping-pong sequence
68
  gif_path = os.path.join(GENERATED_FILES_DIR, "output_enhanced.gif")
69
  images_pil = [Image.fromarray(img) for img in images]
70
  imageio.mimsave(gif_path, images_pil, duration=duration, loop=loop)
71
  return gif_path
72
+
73
  def process_image(image, grid_cols_input, grid_rows_input):
74
  frames = split_image_grid(image, grid_cols_input, grid_rows_input)
75
  zip_file = zip_images(frames)
76
  return zip_file
77
 
78
+ def process_image_to_gif(image, grid_cols_input, grid_rows_input, fps_input, ping_pong_toggle, interpolation_factor):
79
  frames = split_image_grid(image, grid_cols_input, grid_rows_input)
80
+ interpolated_frames = interpolate_frames(frames, interpolation_factor)
81
  enhanced_frames = enhance_gif(interpolated_frames)
82
+ gif_file = create_gif_imageio(enhanced_frames, fps=fps_input, ping_pong_animation=ping_pong_toggle, loop=0)
83
 
84
  # Preview the first frame of the GIF
85
  # preview_image = Image.fromarray(enhanced_frames[0])
 
105
  grid_cols_input = gr.Slider(1, 10, value=2, step=1, label="Grid Columns")
106
  grid_rows_input = gr.Slider(1, 10, value=2, step=1, label="Grid Rows")
107
  fps_input = gr.Slider(1, 30, value=10, step=1, label="FPS (Frames per Second)")
108
+ ping_pong_toggle = gr.Checkbox(label="Ping-Pong Effect")
109
+ interpolation_factor_input = gr.Slider(1, 10, value=2, step=1, label="Interpolation Factor")
110
+
111
  with gr.Row():
112
  zip_button = gr.Button("Create Zip File")
113
  gif_button = gr.Button("Create GIF")
 
118
  zip_button.click(process_image, inputs=[image_input, grid_cols_input, grid_rows_input], outputs=zip_output)
119
  gif_button.click(
120
  process_image_to_gif,
121
+ inputs=[image_input, grid_cols_input, grid_rows_input, fps_input, ping_pong_toggle, interpolation_factor_input],
122
  outputs=[preview_image, gif_output]
123
  )
124