Jeffgold commited on
Commit
41953ff
·
verified ·
1 Parent(s): 61cce60

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -30
app.py CHANGED
@@ -1,26 +1,38 @@
1
  import gradio as gr
2
  from PIL import Image, ImageDraw, ImageOps
3
  import io
4
- import os
5
- import subprocess
6
  from math import cos, radians, sin
7
 
8
- def preprocess_image(image):
9
- # Ensure the image is square by padding it
 
 
 
 
 
 
 
 
 
 
 
 
10
  size = max(image.size)
11
  new_image = Image.new("RGBA", (size, size), (255, 255, 255, 0))
12
  new_image.paste(image, ((size - image.width) // 2, (size - image.height) // 2))
13
  return new_image
14
 
15
- def create_gif(img1, img2, transition_type):
 
16
  frames = []
17
  duration = 100 # Duration for each frame in milliseconds
18
  total_frames = 18 # Total number of frames
19
 
20
  try:
21
- # Preprocess images to make them square and same size
22
- img1 = preprocess_image(img1)
23
- img2 = preprocess_image(img2)
24
 
25
  # Set size for the GIF
26
  size = (256, 256)
@@ -28,36 +40,30 @@ def create_gif(img1, img2, transition_type):
28
  img2 = img2.resize(size, Image.LANCZOS)
29
 
30
  if transition_type == "default":
31
- # Calculate step size for consistent speed
32
  full_width = size[0]
33
- step = full_width // (total_frames // 2) # Divide by 2 as we have 2 parts to the animation
34
 
35
- # Generate frames from left to right
36
  for i in range(0, full_width, step):
37
  frame = Image.new('RGBA', size)
38
  frame.paste(img1, (0, 0))
39
  frame.paste(img2.crop((i, 0, full_width, size[1])), (i, 0), mask=img2.crop((i, 0, full_width, size[1])))
40
-
41
  draw = ImageDraw.Draw(frame)
42
  draw.line((i, 0, i, size[1]), fill=(0, 255, 0), width=2)
43
-
44
  frame = frame.convert('P', palette=Image.ADAPTIVE)
45
  frames.append(frame)
46
 
47
- # Generate frames from right to left
48
  for i in range(full_width, step, -step):
49
  frame = Image.new('RGBA', size)
50
  frame.paste(img1, (0, 0))
51
  frame.paste(img2.crop((i, 0, full_width, size[1])), (i, 0), mask=img2.crop((i, 0, full_width, size[1])))
52
-
53
  draw = ImageDraw.Draw(frame)
54
  draw.line((i, 0, i, size[1]), fill=(0, 255, 0), width=2)
55
-
56
  frame = frame.convert('P', palette=Image.ADAPTIVE)
57
  frames.append(frame)
58
 
59
  elif transition_type == "rotate":
60
- # Create a mask that is 2x the size of the image
61
  mask_size = (size[0] * 2, size[1] * 2)
62
  mask = Image.new('L', mask_size, 0)
63
  draw = ImageDraw.Draw(mask)
@@ -91,8 +97,10 @@ def create_gif(img1, img2, transition_type):
91
  except Exception as e:
92
  raise ValueError(f"Error creating GIF: {e}")
93
 
94
- def create_gif_gradio(image1, image2, transition_type):
95
- gif_data = create_gif(image1, image2, transition_type)
 
 
96
 
97
  # Save the GIF to a temporary file
98
  temp_output_path = "output.gif"
@@ -102,17 +110,38 @@ def create_gif_gradio(image1, image2, transition_type):
102
  return temp_output_path
103
 
104
  # Gradio interface
105
- iface = gr.Interface(
106
- fn=create_gif_gradio,
107
- inputs=[
108
- gr.Image(type="pil", label="Image 1"),
109
- gr.Image(type="pil", label="Image 2"),
110
- gr.Radio(["default", "rotate"], label="Transition Type")
111
- ],
112
- outputs=gr.Image(type="filepath", label="Generated GIF"),
113
- title="GIF Generator",
114
- description="Upload two images and select a transition type to generate an animated GIF."
115
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
 
117
  # Launch the interface
118
  iface.launch()
 
1
  import gradio as gr
2
  from PIL import Image, ImageDraw, ImageOps
3
  import io
4
+ import numpy as np
 
5
  from math import cos, radians, sin
6
 
7
+ def preprocess_image(image, scale, crop_top, crop_left, crop_bottom, crop_right):
8
+ # Scale the image
9
+ new_size = (int(image.width * scale), int(image.height * scale))
10
+ image = image.resize(new_size, Image.LANCZOS)
11
+
12
+ # Crop the image
13
+ width, height = image.size
14
+ left = int(width * crop_left)
15
+ top = int(height * crop_top)
16
+ right = int(width * (1 - crop_right))
17
+ bottom = int(height * (1 - crop_bottom))
18
+ image = image.crop((left, top, right, bottom))
19
+
20
+ # Ensure the image is square by padding
21
  size = max(image.size)
22
  new_image = Image.new("RGBA", (size, size), (255, 255, 255, 0))
23
  new_image.paste(image, ((size - image.width) // 2, (size - image.height) // 2))
24
  return new_image
25
 
26
+ def create_gif(img1, img2, transition_type, scale1, crop_top1, crop_left1, crop_bottom1, crop_right1,
27
+ scale2, crop_top2, crop_left2, crop_bottom2, crop_right2):
28
  frames = []
29
  duration = 100 # Duration for each frame in milliseconds
30
  total_frames = 18 # Total number of frames
31
 
32
  try:
33
+ # Preprocess images with scaling and cropping
34
+ img1 = preprocess_image(img1, scale1, crop_top1, crop_left1, crop_bottom1, crop_right1)
35
+ img2 = preprocess_image(img2, scale2, crop_top2, crop_left2, crop_bottom2, crop_right2)
36
 
37
  # Set size for the GIF
38
  size = (256, 256)
 
40
  img2 = img2.resize(size, Image.LANCZOS)
41
 
42
  if transition_type == "default":
43
+ # Default sliding transition
44
  full_width = size[0]
45
+ step = full_width // (total_frames // 2)
46
 
 
47
  for i in range(0, full_width, step):
48
  frame = Image.new('RGBA', size)
49
  frame.paste(img1, (0, 0))
50
  frame.paste(img2.crop((i, 0, full_width, size[1])), (i, 0), mask=img2.crop((i, 0, full_width, size[1])))
 
51
  draw = ImageDraw.Draw(frame)
52
  draw.line((i, 0, i, size[1]), fill=(0, 255, 0), width=2)
 
53
  frame = frame.convert('P', palette=Image.ADAPTIVE)
54
  frames.append(frame)
55
 
 
56
  for i in range(full_width, step, -step):
57
  frame = Image.new('RGBA', size)
58
  frame.paste(img1, (0, 0))
59
  frame.paste(img2.crop((i, 0, full_width, size[1])), (i, 0), mask=img2.crop((i, 0, full_width, size[1])))
 
60
  draw = ImageDraw.Draw(frame)
61
  draw.line((i, 0, i, size[1]), fill=(0, 255, 0), width=2)
 
62
  frame = frame.convert('P', palette=Image.ADAPTIVE)
63
  frames.append(frame)
64
 
65
  elif transition_type == "rotate":
66
+ # Rotating transition
67
  mask_size = (size[0] * 2, size[1] * 2)
68
  mask = Image.new('L', mask_size, 0)
69
  draw = ImageDraw.Draw(mask)
 
97
  except Exception as e:
98
  raise ValueError(f"Error creating GIF: {e}")
99
 
100
+ def create_gif_gradio(image1, image2, transition_type, scale1, crop_top1, crop_left1, crop_bottom1, crop_right1,
101
+ scale2, crop_top2, crop_left2, crop_bottom2, crop_right2):
102
+ gif_data = create_gif(image1, image2, transition_type, scale1, crop_top1, crop_left1, crop_bottom1, crop_right1,
103
+ scale2, crop_top2, crop_left2, crop_bottom2, crop_right2)
104
 
105
  # Save the GIF to a temporary file
106
  temp_output_path = "output.gif"
 
110
  return temp_output_path
111
 
112
  # Gradio interface
113
+ with gr.Blocks() as iface:
114
+ gr.Markdown("# GIF Generator with Image Scaling and Cropping")
115
+
116
+ with gr.Row():
117
+ with gr.Column():
118
+ image1 = gr.Image(type="pil", label="Image 1")
119
+ scale1 = gr.Slider(minimum=0.1, maximum=2.0, value=1.0, step=0.1, label="Scale Image 1")
120
+ crop_top1 = gr.Slider(minimum=0, maximum=0.5, value=0, step=0.01, label="Crop Top (Image 1)")
121
+ crop_left1 = gr.Slider(minimum=0, maximum=0.5, value=0, step=0.01, label="Crop Left (Image 1)")
122
+ crop_bottom1 = gr.Slider(minimum=0, maximum=0.5, value=0, step=0.01, label="Crop Bottom (Image 1)")
123
+ crop_right1 = gr.Slider(minimum=0, maximum=0.5, value=0, step=0.01, label="Crop Right (Image 1)")
124
+
125
+ with gr.Column():
126
+ image2 = gr.Image(type="pil", label="Image 2")
127
+ scale2 = gr.Slider(minimum=0.1, maximum=2.0, value=1.0, step=0.1, label="Scale Image 2")
128
+ crop_top2 = gr.Slider(minimum=0, maximum=0.5, value=0, step=0.01, label="Crop Top (Image 2)")
129
+ crop_left2 = gr.Slider(minimum=0, maximum=0.5, value=0, step=0.01, label="Crop Left (Image 2)")
130
+ crop_bottom2 = gr.Slider(minimum=0, maximum=0.5, value=0, step=0.01, label="Crop Bottom (Image 2)")
131
+ crop_right2 = gr.Slider(minimum=0, maximum=0.5, value=0, step=0.01, label="Crop Right (Image 2)")
132
+
133
+ transition_type = gr.Radio(["default", "rotate"], label="Transition Type", value="default")
134
+
135
+ generate_button = gr.Button("Generate GIF")
136
+ output_gif = gr.Image(type="filepath", label="Generated GIF")
137
+
138
+ generate_button.click(
139
+ create_gif_gradio,
140
+ inputs=[image1, image2, transition_type,
141
+ scale1, crop_top1, crop_left1, crop_bottom1, crop_right1,
142
+ scale2, crop_top2, crop_left2, crop_bottom2, crop_right2],
143
+ outputs=output_gif
144
+ )
145
 
146
  # Launch the interface
147
  iface.launch()