Spaces:
Sleeping
Sleeping
生成影片
Browse files- image_examples.py +9 -7
image_examples.py
CHANGED
|
@@ -236,6 +236,7 @@ def generate_video_from_image(image_path):
|
|
| 236 |
import io
|
| 237 |
import time
|
| 238 |
from PIL import Image
|
|
|
|
| 239 |
|
| 240 |
# 讀取圖片
|
| 241 |
with open(image_path, "rb") as f:
|
|
@@ -245,14 +246,15 @@ def generate_video_from_image(image_path):
|
|
| 245 |
# 這裡你可以根據圖片內容自訂 prompt,也可以讓 prompt 固定
|
| 246 |
prompt = "請根據這張圖片生成一段有創意的短影片,內容需與圖片主題高度相關。"
|
| 247 |
|
| 248 |
-
# 呼叫 Veo
|
| 249 |
operation = client.models.generate_videos(
|
| 250 |
model="veo-2.0-generate-001",
|
| 251 |
prompt=prompt,
|
| 252 |
-
|
| 253 |
config=types.GenerateVideosConfig(
|
| 254 |
-
person_generation="dont_allow",
|
| 255 |
aspect_ratio="16:9",
|
|
|
|
| 256 |
),
|
| 257 |
)
|
| 258 |
|
|
@@ -263,10 +265,10 @@ def generate_video_from_image(image_path):
|
|
| 263 |
|
| 264 |
# 儲存影片到 images 目錄
|
| 265 |
image_dir = os.path.join(os.getcwd(), "images")
|
| 266 |
-
for n,
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
return os.path.join(image_dir,
|
| 270 |
|
| 271 |
# 若失敗則回傳原圖
|
| 272 |
return image_path
|
|
|
|
| 236 |
import io
|
| 237 |
import time
|
| 238 |
from PIL import Image
|
| 239 |
+
import os
|
| 240 |
|
| 241 |
# 讀取圖片
|
| 242 |
with open(image_path, "rb") as f:
|
|
|
|
| 246 |
# 這裡你可以根據圖片內容自訂 prompt,也可以讓 prompt 固定
|
| 247 |
prompt = "請根據這張圖片生成一段有創意的短影片,內容需與圖片主題高度相關。"
|
| 248 |
|
| 249 |
+
# 呼叫 Veo 影片生成(注意 image 參數)
|
| 250 |
operation = client.models.generate_videos(
|
| 251 |
model="veo-2.0-generate-001",
|
| 252 |
prompt=prompt,
|
| 253 |
+
image=image,
|
| 254 |
config=types.GenerateVideosConfig(
|
| 255 |
+
person_generation="dont_allow",
|
| 256 |
aspect_ratio="16:9",
|
| 257 |
+
number_of_videos=1
|
| 258 |
),
|
| 259 |
)
|
| 260 |
|
|
|
|
| 265 |
|
| 266 |
# 儲存影片到 images 目錄
|
| 267 |
image_dir = os.path.join(os.getcwd(), "images")
|
| 268 |
+
for n, video in enumerate(operation.response.generated_videos):
|
| 269 |
+
fname = f"{os.path.splitext(os.path.basename(image_path))[0]}_veo{n}.mp4"
|
| 270 |
+
video.video.save(os.path.join(image_dir, fname))
|
| 271 |
+
return os.path.join(image_dir, fname)
|
| 272 |
|
| 273 |
# 若失敗則回傳原圖
|
| 274 |
return image_path
|