谷歌发布 Gemini 2.0 flash experimental 模型支持图像生成与编辑
谷歌最近发布了 Gemini 2.0 flash experimental
模型支持图像生成与编辑,而且支持免费的 API
调用,以下主要介绍 API
的调用,想了解更多的内容可以关注我的公众号查询。
Colab 调用
以下演示如何通过 Google Colab
调用使用最新的模型来生成图片
导入 API KEY
from google.colab import userdata
import os
#GEMINI
os.environ["GOOGLE_API_KEY"] = userdata.get('GOOGLE_KEY')
安装 SDK
!pip install google-genai
查询当前免费的模型
注意新版本的写法已经做了调整
import os
from google import genai
from google.genai import types
client = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))
for model in client.models.list():
print(model)
可以看到已经支持 gemini-2.0-flash-exp-image-generation
图片展示组件
由于等下返回的图片中是一个blob,需要在 colab
中展示图片就需要做些调整,而且平台不支持使用 PIL
来显示图片,所以我们需要使用到 cv2
# 一般平台已经安装好的,如果运行代码时提示没安装,就需要安装一下
!pip install pillow opencv-python
代码测试
以下代码测试故事主角的一致性,提示词是生成一个关于一只可爱的婴儿龟的3D数字艺术风格的故事,并且为每个场景生成一张图。
import os
from google import genai
from google.genai import types
from PIL import Image
from io import BytesIO
import numpy as np
import cv2
from google.colab.patches import cv2_imshow
client = genai.Client(api_key=os.getenv("GOOGLE_API_KEY"))
response = client.models.generate_content(
model="gemini-2.0-flash-exp",
contents=(
"Generate a story about a cute baby turtle in a 3d digital art style. "
"For each scene, generate an image."
),
# 注意这里的写法
config=types.GenerateContentConfig(
response_modalities=["Text", "Image"]
),
)
for part in response.candidates[0].content.parts:
if part.text is not None:
print(part.text)
elif part.inline_data is not None:
nparr = np.frombuffer(part.inline_data.data, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR) # OpenCV 用于解码图像
cv2_imshow(img)
# 官方示例代码的这种方式不能展示图片,因为colab不支持
# image = Image.open(BytesIO(part.inline_data.data))
# image.show()
效果
Once upon a time, nestled amongst smooth, sun-warmed river stones, lived a tiny turtle named Pip. He wasn't just any turtle; Pip had a shell the color of fresh moss and eyes like shiny black beads. He was the cutest baby turtle you ever did see.
从前,在一片光滑、温暖的河石中,住着一只名叫皮普的小乌龟。他不是一只普通的乌龟;皮普的壳是新鲜苔藓的颜色,眼睛像闪亮的黑色珠子。他是你见过的最可爱的婴儿乌龟。
One sunny morning, a bright red ladybug landed right on Pip's nose! Pip blinked his big eyes, surprised but not scared. The ladybug tickled, and Pip let out a tiny squeak that sounded like a gentle chirp.
一天阳光明媚的早晨,一只鲜红的瓢虫正好落在皮普的鼻子上!皮普眨着他大的眼睛,感到惊讶但并不害怕。瓢虫的轻触让皮普发出了一声微小的尖叫,听起来像是一种温柔的啁啾声。
Feeling adventurous, Pip decided to explore beyond his usual patch of stones. He slowly paddled into the cool river water. The sunlight danced on the surface, creating shimmering patterns that fascinated him.
感觉冒险精神,皮普决定探索超出他通常的石头地界。他慢慢地划入凉爽的河水中。阳光在水面上跳跃,创造出闪闪发光的图案,让他着迷。
Under a large, lily pad, Pip spotted a school of tiny, silver fish darting this way and that. He watched them with wide-eyed curiosity, their movements like a sparkling underwater ballet.
在一个很大的荷叶下,皮普发现了一群小小的银色鱼在快速地游来游去。他带着极大的好奇心看着它们,它们的动作就像一场闪闪发光的水下芭蕾舞。
As the sun began to dip below the horizon, painting the sky in hues of orange and pink, Pip felt a little sleepy. He found a cozy spot nestled amongst the roots of an old willow tree, its long branches dipping gently into the water.
当太阳开始沉入地平线下,将天空涂抹上橙色和粉色的色调时,皮普感到有点困。他找到一个舒适的休息处,位于一棵老柳树根部的缝隙中,它的长枝条轻轻地探入水中。
Curled up in his safe haven, Pip closed his big, black eyes and dreamed of ladybugs, shimmering fish, and all the wonderful things he would explore tomorrow. The river gurgled a soft lullaby, and the cute little turtle drifted off to sleep, ready for more adventures.
卷曲在他安全的港湾里,皮普闭上了他大的、黑色的眼睛,梦见了瓢虫、闪闪发光的鱼,以及他明天将要探索的所有美好的事物。河流轻轻地发出摇篮曲的声音,小乌龟也随之入睡,准备迎接更多的冒险。
版权声明:
作者:lrbmike
链接:https://blog.liurb.org/2025/03/15/gemini-2-0-flash-experimental/
来源:大卷学长
文章版权归作者所有,未经允许请勿转载。