Files
baijiahao_text_to_image/11111111.py

32 lines
1.4 KiB
Python

from google import genai
from google.genai import types
from PIL import Image
from google.genai.client import HttpOptions
client = genai.Client(http_options=HttpOptions(base_url="https://work.poloapi.com"),api_key="sk-V4tPnDgzFPa7nxWrvKnNJsW8ZcBXXPuGmjfgvPVRnwpHoeob")
prompt = ("Create a picture of a nano banana dish in a fancy restaurant with a Gemini theme")
response = client.models.generate_content(
model="gemini-3-pro-image-preview",
contents=[prompt],
)
# 检查是否有候选答案
if not response.candidates:
print("API未返回任何候选答案")
else:
candidate = response.candidates[0]
if not candidate.content:
print("API返回的候选答案中没有内容")
elif not hasattr(candidate.content, 'parts') or not candidate.content.parts:
print("API返回的候选答案内容中没有parts")
else:
for part in candidate.content.parts:
if hasattr(part, 'text') and part.text is not None:
print(part.text)
elif hasattr(part, 'inline_data') and part.inline_data is not None:
image_data = part.inline_data
if image_data.data is not None:
# 保存图片数据到文件
with open('generated_image.png', 'wb') as f:
f.write(image_data.data)
print("图片生成成功: generated_image.png")