图片生成(文本转图片)
from google import genai from google.genai import types from PIL import Image client = genai.Client() prompt = ( "Create a picture of a nano banana dish in a fancy restaurant with a Gemini theme" ) response = client.models.generate_content( model="gemini-2.5-flash-image", contents=[prompt], ) for part in response.parts: if part.text is not None: print(part.text) elif part.inline_data is not None: image = part.as_image() image.save("generated_image.png")注意:python要提前安装Pillow
pip install Pillow图片编辑(文字和图片转图片)
from google import genai from google.genai import types from PIL import Image client = genai.Client() prompt = ( "Create a picture of my cat eating a nano-banana in a " "fancy restaurant under the Gemini constellation", ) image = Image.open("/path/to/cat_image.png") response = client.models.generate_content( model="gemini-2.5-flash-image", contents=[prompt, image], ) for part in response.parts: if part.text is not None: print(part.text) elif part.inline_data is not None: image = part.as_image() image.save("generated_image.png")Gemini 3 Pro Image (gemini-3-pro-image-preview) 是一款先进的图片生成和编辑模型,针对专业资源制作进行了优化。Gemini 1.5 Pro 旨在通过高级推理来应对最具挑战性的工作流程,擅长处理复杂的多轮创建和修改任务。
- 高分辨率输出:内置 1K、2K 和 4K 视觉效果生成功能。
- 高级文字渲染:能够为信息图表、菜单、图表和营销素材资源生成清晰易读的风格化文字。
- 使用 Google 搜索进行接地:模型可以使用 Google 搜索作为工具来验证事实,并根据实时数据(例如当前天气地图、股票图表、近期活动)生成图像。
- 思考模式:模型会利用“思考”过程来推理复杂的提示。它会生成临时“思维图像”(在后端可见,但不收费),以在生成最终的高质量输出之前优化构图。
- 最多 14 张参考图片:您现在最多可以混合使用 14 张参考图片来生成最终图片。
最多可使用 14 张参考图片
借助 Gemini 3 Pro 预览版,您最多可以混合 14 张参考图片。这 14 张图片可以包含以下内容:
- 最多 6 张高保真对象图片,用于包含在最终图片中
最多 5 张人像照片,以保持角色一致性
from google import genai from google.genai import types from PIL import Image prompt = "An office group photo of these people, they are making funny faces." aspect_ratio = "5:4" # "1:1","2:3","3:2","3:4","4:3","4:5","5:4","9:16","16:9","21:9" resolution = "2K" # "1K", "2K", "4K" client = genai.Client() response = client.models.generate_content( model="gemini-3-pro-image-preview", contents=[ prompt, Image.open('person1.png'), Image.open('person2.png'), Image.open('person3.png'), Image.open('person4.png'), Image.open('person5.png'), ], config=types.GenerateContentConfig( response_modalities=['TEXT', 'IMAGE'], image_config=types.ImageConfig( aspect_ratio=aspect_ratio, image_size=resolution ), ) ) for part in response.parts: if part.text is not None: print(part.text) elif image:= part.as_image(): image.save("office.png")