Dall-E 由 OpenAI 发布,是一种基于深度学习的生成模型,它是一种改进的 GPT 模型,专门用于图像生成。Dall-E 可以根据文本描述生成与之相符合的原创性、真实的图像。
Dall-E 通过一个拥有 120 亿参数的 Transformer 模型来理解自然语言的输入,并生成相应的图片。这些图片不仅限于现实中已存在的图像,还包括虚拟的、现实中不存在的图像。
Dall-E 和 Dall-E 2
Dall-E 最早发布于 2021 年 1 月 5 日由 OpenAI 发布,它具备生成逼真图像的能力。一年后的 2022 年 4 月 OpenAI 宣布了新版本的 DALL-E 2,这一版本具备更为强大的功能,并且在分辨率方便也提高了 4 倍。以下是 Dall-E 所具备的扩展的功能:
Dall-E

Dall-E 2

Dall-E 2 的局限性
尽管 Dall-E 2 的功能已经非常强大,然而开发者也公开提出了它的局限性,对于图像的属性,Dall-E 2 是没有一个很准确的判断的,并且细节方面还有很多的欠缺。
并且基于安全考虑,Dall-E 2 是不会生成包含暴力、政治等敏感图片的。
Dall-E 3 的增强
相比较 Dall-E 2 的图像生成,Dall-E 3 对图像的增强有以下几个方面:
standard标准与HD高清两种。Dall-E 2

Dall-E 3

OpenAI 提供了三种 API 调用的方式,如下所示:
前提:已安装 openai 库
保存图片需要提前下载 requests 库:pip install requests
# 实例化 openai 的对象client = OpenAI(base_url="xxx",api_key="xxxx")def generate_image_path():# 生成图片路径return os.path.join("img_" + datetime.datetime.now().strftime("%Y%m%d%H%M%S") + ".png")def test_image_normal():# 文生图,指定模型,给出提示语和大小限制response = client.images.generate(model="dall-e-3",prompt="一只猫在窗户边睡觉",size="1024x1024",)# 得到生成的图片链接image_url = response.data[0].urlprint(image_url)# 下载并保存图像image_response = requests.get(image_url)with open(generate_image_path(), 'wb') as f:f.write(image_response.content)
增加图像生成的条件
def test_image_style():# 文生图response = client.images.generate(model="dall-e-3",prompt="一只猫在窗户边睡觉",size="1024x1024",style="natural",quality="standard",n=1)image_url = response.data[0].urlprint(image_url)
生成多个图像
def test_image_num():response = client.images.generate(model="dall-e-2",prompt="一只猫在窗户边睡觉",n=3)for i, image in enumerate(response.data):image_response = requests.get(image.url)with open(generate_image_path(), 'wb') as f:f.write(image_response.content)print(f"生成的第{i}张图片地址是:{image.url}")
下载 img1.png下载 img2.png
def test_change_image():# 将图1根据提示在图2的标记上进行修改response = client.images.edit(model="dall-e-2",image=open("img1.png", "rb"),mask=open('img2.png', 'rb'),prompt="A sunlit indoor lounge area with a pool containing a flamingo",n=1,size="256x256")# 生成的图像路径image_url = response.data[0].url# 存储图片image_response = requests.get(image_url)with open(generate_image_path(), 'wb') as f:f.write(image_response.content)
下载ori_img.png
def test_variation_image():response = client.images.create_variation(model="dall-e-2",# 给出原图像image=open("ori_img.png", "rb"),n=1,size="1024x1024",)# 获取生成的图片路径image_url = response.data[0].url# 保存图片image_response = requests.get(image_url)with open(generate_image_path(), 'wb') as f:f.write(image_response.content)
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号