@@ -109,38 +109,76 @@ def zoom(image, zoom_factor, center=None):
109
109
return resized
110
110
111
111
112
+ # def zoom(image, factor, center):
113
+ # height, width, _ = image.shape
114
+ # if center is None:
115
+ # center = (width // 2, height // 2)
116
+
117
+ # x, y = center
118
+ # new_width = int(width / factor)
119
+ # new_height = int(height / factor)
120
+ # left = max(0, x - new_width // 2)
121
+ # right = min(width, x + new_width // 2)
122
+ # top = max(0, y - new_height // 2)
123
+ # bottom = min(height, y + new_height // 2)
124
+ # cropped_image = image[top:bottom, left:right]
125
+ # resized_image = cv2.resize(cropped_image, (width, height), interpolation=cv2.INTER_LINEAR)
126
+ # return resized_image
127
+
128
+
112
129
@agent_action
113
- def zoom_at (
114
- image_filename , duration , zoom_factor , output_filename , center = None , fps = 30
130
+ def zoom_to (
131
+ image_filename ,
132
+ output_filename ,
133
+ duration ,
134
+ zoom_factor_from ,
135
+ zoom_factor_to ,
136
+ zoom_start_position ,
137
+ zoom_end_position ,
138
+ fps = 30 ,
115
139
):
116
140
"""
117
141
Creates a zoom-in video clip from an image.
118
142
119
143
Args:
120
144
image_filename (str): Path to the input image.
121
- duration (int): Duration of the video in seconds.
122
- zoom_factor (float): Maximum zoom factor.
123
145
output_filename (str): Path to save the output video.
124
- center (tuple, optional): Center point for zooming. Defaults to image center, is in pixels (512, 512) or as a ratio (0.5, 0.5).
146
+ duration (int): Duration of the video in seconds.
147
+ zoom_factor_from (float): Starting zoom factor.
148
+ zoom_factor_to (float): Ending zoom factor.
149
+ zoom_start_position (tuple): Starting center point for zooming.
150
+ zoom_end_position (tuple): Ending center point for zooming.
125
151
fps (int): Frames per second. Defaults to 30.
126
152
127
153
Returns:
128
154
str: Path to the saved video clip.
129
155
"""
130
- zoom_factor = float (zoom_factor )
156
+ zoom_factor_from = float (zoom_factor_from )
157
+ zoom_factor_to = float (zoom_factor_to )
131
158
duration = int (duration )
132
159
fps = int (fps )
133
160
image_path = os .path .join (GlobalValues .ASSISTANTS_WORKING_FOLDER , image_filename )
134
161
image = cv2 .imread (image_path )
135
162
image = cv2 .cvtColor (image , cv2 .COLOR_BGR2RGB )
136
163
height , width , _ = image .shape
137
164
image_dim = (width , height )
138
- if center :
139
- center = convert_and_validate (center , image_dim )
165
+ if zoom_start_position :
166
+ zoom_start_position = convert_and_validate (zoom_start_position , image_dim )
167
+ if zoom_end_position :
168
+ zoom_end_position = convert_and_validate (zoom_end_position , image_dim )
140
169
141
170
def make_frame (t ):
142
171
frame_num = int (t * fps )
143
- factor = 1 + (zoom_factor - 1 ) * frame_num / (fps * duration )
172
+ factor = zoom_factor_from + (zoom_factor_to - zoom_factor_from ) * frame_num / (
173
+ fps * duration
174
+ )
175
+ center_x = zoom_start_position [0 ] + (
176
+ zoom_end_position [0 ] - zoom_start_position [0 ]
177
+ ) * frame_num / (fps * duration )
178
+ center_y = zoom_start_position [1 ] + (
179
+ zoom_end_position [1 ] - zoom_start_position [1 ]
180
+ ) * frame_num / (fps * duration )
181
+ center = (int (center_x ), int (center_y ))
144
182
return zoom (image , factor , center )
145
183
146
184
clip = VideoClip (make_frame , duration = duration )
@@ -227,43 +265,6 @@ def make_frame(t):
227
265
return output_filename
228
266
229
267
230
- @agent_action
231
- def zoom_from (
232
- image_filename , duration , zoom_factor , output_filename , center = None , fps = 30
233
- ):
234
- """
235
- Creates a zoom-out video clip from an image.
236
-
237
- Args:
238
- image_filename (str): Path to the input image.
239
- duration (int): Duration of the video in seconds.
240
- zoom_factor (float): Initial zoom factor.
241
- output_filename (str): Path to save the output video.
242
- center (tuple, optional): Center point for zooming. Defaults to image center, is in pixels (512, 512) or as a ratio (0.5, 0.5).
243
- fps (int): Frames per second. Defaults to 30.
244
-
245
- Returns:
246
- str: Path to the saved video clip.
247
- """
248
- duration = int (duration )
249
- zoom_factor = float (zoom_factor )
250
- fps = int (fps )
251
- image_path = os .path .join (GlobalValues .ASSISTANTS_WORKING_FOLDER , image_filename )
252
- image = cv2 .imread (image_path )
253
- image = cv2 .cvtColor (image , cv2 .COLOR_BGR2RGB )
254
- height , width , _ = image .shape
255
-
256
- def make_frame (t ):
257
- frame_num = int (t * fps )
258
- factor = zoom_factor - (zoom_factor - 1 ) * frame_num / (fps * duration )
259
- return zoom (image , factor , center )
260
-
261
- clip = VideoClip (make_frame , duration = duration )
262
- output_path = os .path .join (GlobalValues .ASSISTANTS_WORKING_FOLDER , output_filename )
263
- clip .write_videofile (output_path , fps = fps )
264
- return output_filename
265
-
266
-
267
268
@agent_action
268
269
def boom_to (
269
270
image_filename , duration , start_center , end_center , output_filename , fps = 30
0 commit comments