Update Image_generation.py
Browse files- Image_generation.py +71 -42
Image_generation.py
CHANGED
@@ -1,47 +1,76 @@
|
|
1 |
import os
|
2 |
import io
|
3 |
-
import PIL.Image
|
4 |
import tempfile
|
5 |
-
from google import genai
|
6 |
-
from google.genai import types
|
7 |
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
model="gemini-2.0-flash-exp-image-generation",
|
15 |
-
contents=prompt,
|
16 |
-
config=types.GenerateContentConfig(response_modalities=['Text', 'Image'])
|
17 |
-
)
|
18 |
-
|
19 |
-
for part in response.candidates[0].content.parts:
|
20 |
-
if part.text is not None:
|
21 |
-
print(part.text)
|
22 |
-
elif part.inline_data is not None:
|
23 |
-
return part.inline_data.data
|
24 |
-
|
25 |
-
return
|
26 |
-
|
27 |
-
def upload_image_to_imgur(client, image_binary, album=None, name="gemini-image", title="gemini Generated Image"):
|
28 |
-
# 將 binary 資料轉為 PIL Image
|
29 |
-
image = PIL.Image.open(io.BytesIO(image_binary))
|
30 |
-
|
31 |
-
# 建立暫存檔案來上傳 (因為 ImgurClient 需要檔案路徑)
|
32 |
-
with tempfile.NamedTemporaryFile(suffix=".png", delete=True) as tmp:
|
33 |
-
image.save(tmp.name, format='PNG')
|
34 |
-
|
35 |
-
# 準備上傳資訊
|
36 |
-
config = {
|
37 |
-
'album': album,
|
38 |
-
'name': name,
|
39 |
-
'title': title,
|
40 |
-
'description': f'Generated by gemini - {datetime.now()}'
|
41 |
-
}
|
42 |
-
|
43 |
-
# 使用 client 進行圖片上傳
|
44 |
-
uploaded_image = client.upload_from_path(tmp.name, config=config, anon=False)
|
45 |
-
|
46 |
-
# 回傳圖片網址
|
47 |
-
return uploaded_image['link']
|
|
|
1 |
import os
|
2 |
import io
|
|
|
3 |
import tempfile
|
|
|
|
|
4 |
from datetime import datetime
|
5 |
+
import PIL.Image
|
6 |
+
import google.generativeai as genai
|
7 |
+
from google.generativeai import types
|
8 |
+
from imgurpython import ImgurClient
|
9 |
+
|
10 |
+
class ImageGenerator:
|
11 |
+
def __init__(self, google_api_key, imgur_client_id, imgur_client_secret, imgur_access_token, imgur_refresh_token):
|
12 |
+
# 初始化 Gemini 客戶端
|
13 |
+
self.genai_client = genai.Client(api_key=google_api_key)
|
14 |
+
# 初始化 Imgur 客戶端
|
15 |
+
self.imgur_client = ImgurClient(imgur_client_id, imgur_client_secret, imgur_access_token, imgur_refresh_token)
|
16 |
+
|
17 |
+
def generate_image_with_gemini(self, prompt):
|
18 |
+
"""
|
19 |
+
使用 Gemini 模型生成圖片。
|
20 |
+
|
21 |
+
參數:
|
22 |
+
prompt (str): 用於生成圖片的提示詞。
|
23 |
+
|
24 |
+
返回:
|
25 |
+
bytes: 生成的圖片的二進位資料,如果生成失敗則返回 None。
|
26 |
+
"""
|
27 |
+
response = self.genai_client.models.generate_content(
|
28 |
+
model="gemini-2.0-flash-exp-image-generation",
|
29 |
+
contents=prompt,
|
30 |
+
config=types.GenerateContentConfig(response_modalities=['Text', 'Image'])
|
31 |
+
)
|
32 |
+
|
33 |
+
for part in response.candidates[0].content.parts:
|
34 |
+
if part.text is not None:
|
35 |
+
print(part.text)
|
36 |
+
elif part.inline_data is not None:
|
37 |
+
return part.inline_data.data
|
38 |
+
return None
|
39 |
+
|
40 |
+
def upload_image_to_imgur(self, image_binary, album=None, name="gemini-image", title="Gemini Generated Image"):
|
41 |
+
"""
|
42 |
+
將圖片上傳到 Imgur。
|
43 |
+
|
44 |
+
參數:
|
45 |
+
image_binary (bytes): 要上傳的圖片的二進位資料。
|
46 |
+
album (str): 要上傳到的相簿 ID,預設為 None。
|
47 |
+
name (str): 圖片的名稱,預設為 "gemini-image"。
|
48 |
+
title (str): 圖片的標題,預設為 "Gemini Generated Image"。
|
49 |
+
|
50 |
+
返回:
|
51 |
+
str: 上傳後的圖片 URL,如果上傳失敗則返回 None。
|
52 |
+
"""
|
53 |
+
try:
|
54 |
+
# 將二進位資料轉換為 PIL Image
|
55 |
+
image = PIL.Image.open(io.BytesIO(image_binary))
|
56 |
+
|
57 |
+
# 建立暫存檔案以便上傳
|
58 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=True) as tmp:
|
59 |
+
image.save(tmp.name, format='PNG')
|
60 |
+
|
61 |
+
# 準備上傳資訊
|
62 |
+
config = {
|
63 |
+
'album': album,
|
64 |
+
'name': name,
|
65 |
+
'title': title,
|
66 |
+
'description': f'Generated by Gemini - {datetime.now()}'
|
67 |
+
}
|
68 |
+
|
69 |
+
# 使用 Imgur 客戶端上傳圖片
|
70 |
+
uploaded_image = self.imgur_client.upload_from_path(tmp.name, config=config, anon=False)
|
71 |
|
72 |
+
# 返回圖片的連結
|
73 |
+
return uploaded_image['link']
|
74 |
+
except Exception as e:
|
75 |
+
print(f"圖片上傳失敗: {e}")
|
76 |
+
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|