Spaces:
Sleeping
Sleeping
生成影片
Browse files- image_examples.py +55 -1
image_examples.py
CHANGED
|
@@ -136,6 +136,15 @@ def handle_image_message(event):
|
|
| 136 |
"label": "吉卜力風格圖片",
|
| 137 |
"data": f"ghibli,{filename}"
|
| 138 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
}
|
| 140 |
]
|
| 141 |
}
|
|
@@ -180,6 +189,23 @@ def handle_postback(event):
|
|
| 180 |
]
|
| 181 |
)
|
| 182 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
def convert_to_ghibli_style(image_path):
|
| 185 |
from PIL import Image
|
|
@@ -204,4 +230,32 @@ def convert_to_ghibli_style(image_path):
|
|
| 204 |
out_path = image_path.replace(".jpg", "_ghibli.jpg")
|
| 205 |
out_img.save(out_path)
|
| 206 |
return out_path
|
| 207 |
-
return image_path # 若失敗則回傳原圖
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
"label": "吉卜力風格圖片",
|
| 137 |
"data": f"ghibli,{filename}"
|
| 138 |
}
|
| 139 |
+
},
|
| 140 |
+
{
|
| 141 |
+
"type": "button",
|
| 142 |
+
"style": "primary",
|
| 143 |
+
"action": {
|
| 144 |
+
"type": "postback",
|
| 145 |
+
"label": "生成影片",
|
| 146 |
+
"data": f"veo,{filename}"
|
| 147 |
+
}
|
| 148 |
}
|
| 149 |
]
|
| 150 |
}
|
|
|
|
| 189 |
]
|
| 190 |
)
|
| 191 |
)
|
| 192 |
+
elif data.startswith("veo,"):
|
| 193 |
+
_, filename = data.split(",", 1)
|
| 194 |
+
image_dir = os.path.join(os.getcwd(), "images")
|
| 195 |
+
image_path = os.path.join(image_dir, filename)
|
| 196 |
+
# 呼叫 Veo 影片生成
|
| 197 |
+
video_path = generate_video_from_image(image_path)
|
| 198 |
+
video_url = f"https://{base_url}/images/{os.path.basename(video_path)}"
|
| 199 |
+
with ApiClient(configuration) as api_client:
|
| 200 |
+
MessagingApi(api_client).reply_message(
|
| 201 |
+
ReplyMessageRequest(
|
| 202 |
+
reply_token=event.reply_token,
|
| 203 |
+
messages=[
|
| 204 |
+
TextMessage(text="這是根據圖片生成的影片:"),
|
| 205 |
+
TextMessage(text=video_url)
|
| 206 |
+
]
|
| 207 |
+
)
|
| 208 |
+
)
|
| 209 |
|
| 210 |
def convert_to_ghibli_style(image_path):
|
| 211 |
from PIL import Image
|
|
|
|
| 230 |
out_path = image_path.replace(".jpg", "_ghibli.jpg")
|
| 231 |
out_img.save(out_path)
|
| 232 |
return out_path
|
| 233 |
+
return image_path # 若失敗則回傳原圖
|
| 234 |
+
|
| 235 |
+
def generate_video_from_image(image_path):
|
| 236 |
+
import io
|
| 237 |
+
from PIL import Image
|
| 238 |
+
|
| 239 |
+
# 讀取圖片
|
| 240 |
+
with open(image_path, "rb") as f:
|
| 241 |
+
img_bytes = f.read()
|
| 242 |
+
image = Image.open(io.BytesIO(img_bytes))
|
| 243 |
+
|
| 244 |
+
prompt = "請根據這張圖片生成一段有創意的短影片,內容需與圖片主題高度相關,回傳影片檔案。"
|
| 245 |
+
response = client.models.generate_content(
|
| 246 |
+
model="veo-2.0-generate-001",
|
| 247 |
+
contents=[image, prompt],
|
| 248 |
+
config=types.GenerateContentConfig(
|
| 249 |
+
response_modalities=["VIDEO", "TEXT"]
|
| 250 |
+
),
|
| 251 |
+
)
|
| 252 |
+
# 取得 Gemini Veo 回傳的影片
|
| 253 |
+
for part in response.candidates[0].content.parts:
|
| 254 |
+
if part.inline_data is not None:
|
| 255 |
+
video_bytes = part.inline_data.data
|
| 256 |
+
out_path = image_path.replace(".jpg", "_veo.mp4")
|
| 257 |
+
with open(out_path, "wb") as f:
|
| 258 |
+
f.write(video_bytes)
|
| 259 |
+
return out_path
|
| 260 |
+
# 若失敗則回傳原圖
|
| 261 |
+
return image_path
|