You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取通过OpenAI Image API生成的图像历史记录?

问题:通过OpenAI Python图像API生成的图像无法在DALL-E网页UI中检索

我使用OpenAI的Python图像API生成了大量图像,示例代码如下:

response = openai.Image.create(
    prompt=prompt,
    n=n,
    size=size,
    response_format='url'
)

但这些生成的图像无法在DALL-E网页UI中找到。我已经在该UI、API文档及社区线程中搜索过,找到的最接近相关解答的内容来自OpenAI社区讨论。


原因

通过API调用生成的图像(无论使用url还是b64_json格式)不会自动同步到DALL-E网页UI的历史记录中。这是因为API调用和网页UI属于独立的会话体系,网页UI仅保存通过其界面直接生成的图像记录。

解决方法

1. 本地存储图像元数据与URL

调用API时,将生成的图像URL、prompt、生成时间等元数据保存到本地文件(如JSON、CSV),后续检索时直接从本地存储查询。示例代码:

import json
from datetime import datetime

# 生成图像
response = openai.Image.create(
    prompt=prompt,
    n=n,
    size=size,
    response_format='url'
)

# 构造图像记录
image_record = {
    "prompt": prompt,
    "generated_at": datetime.now().isoformat(),
    "image_urls": [item["url"] for item in response["data"]],
    "size": size
}

# 追加到本地JSON文件
with open("dalle_generated_images.json", "a", encoding="utf-8") as f:
    json.dump(image_record, f, ensure_ascii=False)
    f.write("\n")

2. 直接保存图像文件到本地

改用response_format='b64_json'获取图像的Base64编码,直接解码保存为本地图像文件,同时记录相关元数据,方便后续本地检索和管理。示例代码:

import base64
import os
from datetime import datetime

# 生成图像(指定返回Base64编码)
response = openai.Image.create(
    prompt=prompt,
    n=n,
    size=size,
    response_format='b64_json'
)

# 创建保存目录
save_directory = "dalle_images"
os.makedirs(save_directory, exist_ok=True)

# 保存每张图像及元数据
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
for idx, img_item in enumerate(response["data"]):
    # 保存图像文件
    img_filename = f"{timestamp}_image_{idx+1}.png"
    img_path = os.path.join(save_directory, img_filename)
    with open(img_path, "wb") as f:
        f.write(base64.b64decode(img_item["b64_json"]))
    
    # 保存元数据
    meta_filename = f"{timestamp}_meta_{idx+1}.txt"
    meta_path = os.path.join(save_directory, meta_filename)
    with open(meta_path, "w", encoding="utf-8") as f:
        f.write(f"Prompt: {prompt}\n")
        f.write(f"Size: {size}\n")
        f.write(f"Generated Time: {datetime.now().isoformat()}\n")
        f.write(f"Image Path: {img_path}\n")

3. 手动同步到DALL-E UI(可选)

如果需要将API生成的图像纳入DALL-E UI的管理,可手动下载图像后,通过UI的“编辑”功能上传图像。但此方法无法关联原始prompt,仅能保存图像本身。


内容的提问来源于stack exchange,提问作者thedatastrategist

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 00:32:17