#Generates an image Artificial intelligence
1 messages · Page 1 of 1 (latest)
Hi @floral gazelle. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
You can generate that image using OpenAI’s image models (recommended: gpt-image-1) either via the Images API or the Responses API.
1. Using the Images API (Python)
This creates an image from your text prompt and saves it to a file. [Create image; Image generation]
import base64
from openai import OpenAI
client = OpenAI()
prompt = "A photorealistic image of a sleeping cockroach wearing a small hat, peaceful and cute"
img = client.images.generate(
model="gpt-image-1",
prompt=prompt,
n=1,
size="1024x1024"
)
image_bytes = base64.b64decode(img.data[0].b64_json)
with open("sleeping_cockroach_hat.png", "wb") as f:
f.write(image_bytes)
2. Using the Responses API image tool (JavaScript)
Here the model decides when to call the image_generation tool and returns a base64 image. [Images & vision; Image tool]
import OpenAI from "openai";
import { writeFileSync } from "fs";
const client = new OpenAI();
const response = await client.responses.create({
model: "gpt-4.1-mini",
input: "Generate an image of a sleeping cockroach wearing a hat, in a cute, stylized way",
tools: [{ type: "image_generation" }],
});
const imageData = response.output
.filter(o => o.type === "image_generation_call")
.map(o => o.result);
if (imageData.length > 0) {
const imageBase64 = imageData[0];
writeFileSync("sleeping_cockroach_hat.png", Buffer.from(imageBase64, "base64"));
}
In ChatGPT (consumer product), you can simply type a prompt like:
“Create an image of a sleeping cockroach wearing a hat, in a cute illustration style.”