LPX55 commited on
Commit
cb15469
·
verified ·
1 Parent(s): d4b1886

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -15
app.py CHANGED
@@ -1,11 +1,14 @@
1
  import gradio as gr
2
  import numpy as np
3
  from io import BytesIO
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"
@@ -37,21 +40,31 @@ def split_image_grid(image, grid_cols, grid_rows):
37
  frames.append(np.array(frame))
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):
@@ -61,9 +74,22 @@ def process_image(image, grid_cols_input, grid_rows_input):
61
 
62
  def process_image_to_gif(image, grid_cols_input, grid_rows_input):
63
  frames = split_image_grid(image, grid_cols_input, grid_rows_input)
64
- gif_file = create_gif(frames)
 
 
65
  return gif_file
66
 
 
 
 
 
 
 
 
 
 
 
 
67
  with gr.Blocks() as demo:
68
  with gr.Row():
69
  image_input = gr.Image(label="Input Image", type="pil")
 
1
  import gradio as gr
2
  import numpy as np
3
  from io import BytesIO
4
+ from PIL import Image, ImageOps
5
  import zipfile
6
  import os
7
  import atexit
8
  import shutil
9
+ import cv2
10
+ import imageio
11
+ import torchvision.transforms.functional as TF
12
 
13
  # Create a persistent directory to store generated files
14
  GENERATED_FILES_DIR = "generated_files"
 
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:
59
+ img = ImageOps.autocontrast(Image.fromarray(img))
60
+ img = img.convert("RGB") # Ensure the image is in RGB mode
61
+ enhanced_images.append(np.array(img))
62
+ return enhanced_images
63
 
64
+ def create_gif_imageio(images, duration=50, loop=0):
65
+ gif_path = os.path.join(GENERATED_FILES_DIR, "output_enhanced.gif")
66
  images_pil = [Image.fromarray(img) for img in images]
67
+ imageio.mimsave(gif_path, images_pil, duration=duration, loop=loop)
68
  return gif_path
69
 
70
  def process_image(image, grid_cols_input, grid_rows_input):
 
74
 
75
  def process_image_to_gif(image, grid_cols_input, grid_rows_input):
76
  frames = split_image_grid(image, grid_cols_input, grid_rows_input)
77
+ interpolated_frames = interpolate_frames(frames, factor=2)
78
+ enhanced_frames = enhance_gif(interpolated_frames)
79
+ gif_file = create_gif_imageio(enhanced_frames, duration=50, loop=0)
80
  return gif_file
81
 
82
+ def zip_images(images):
83
+ zip_path = os.path.join(GENERATED_FILES_DIR, "output.zip")
84
+ with zipfile.ZipFile(zip_path, 'w') as zipf:
85
+ for idx, img in enumerate(images):
86
+ img_buffer = BytesIO()
87
+ img = Image.fromarray(img)
88
+ img.save(img_buffer, format='PNG')
89
+ img_buffer.seek(0)
90
+ zipf.writestr(f'image_{idx}.png', img_buffer.getvalue())
91
+ return zip_path
92
+
93
  with gr.Blocks() as demo:
94
  with gr.Row():
95
  image_input = gr.Image(label="Input Image", type="pil")