In this guide, you will learn about building applications involving images with the OpenAI API.
If you know what you want to build, find your use case below to get started. If you’re not sure where to start, continue reading to get an overview.
A tour of image-related use cases
Recent language models can process image inputs and analyze them—a capability known as vision. GPT Image models can use text and image inputs to create new images or edit existing ones.
The OpenAI API offers several endpoints to process images as input or generate them as output, enabling you to build powerful multimodal applications.
Analyze images and use them as input to generate text or audio
To learn more about the input and output modalities supported by our models, refer to our models page.
Generate or edit images
You can generate or edit images using the Image API or the Responses API.
The state-of-the-art image generation model, gpt-image-2, can understand text and images and use broad world knowledge to generate images with strong instruction following and contextual awareness.
Generate images with Responses
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20import OpenAI from "openai";const openai = new OpenAI();const response = await openai.responses.create({ model: "gpt-5.6", input: "Generate an image of gray tabby cat hugging an otter with an orange scarf", tools: [{ type: "image_generation" }],});// Save the image to a fileconst imageData = response.output .filter((output) => output.type === "image_generation_call") .map((output) => output.result);if (imageData.length > 0) { const imageBase64 = imageData[0]; const fs = await import("fs"); fs.writeFileSync("cat_and_otter.png", Buffer.from(imageBase64, "base64"));}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22from openai import OpenAIimport base64client = OpenAI()response = client.responses.create(model="gpt-5.6",input="Generate an image of gray tabby cat hugging an otter with an orange scarf",tools=[{"type": "image_generation"}],)# Save the image to a fileimage_data = [ output.resultfor output in response.outputif output.type =="image_generation_call"]if image_data: image_base64 = image_data[0]withopen("cat_and_otter.png", "wb") as f: f.write(base64.b64decode(image_base64))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43package mainimport ( "context" "encoding/base64" "os" "github.com/openai/openai-go/v3" "github.com/openai/openai-go/v3/responses")func main() { client := openai.NewClient() response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{ Model: "gpt-5.6", Input: responses.ResponseNewParamsInputUnion{ OfString: openai.String("Generate an image of a gray tabby cat hugging an otter with an orange scarf."), }, Tools: []responses.ToolUnionParam{{ OfImageGeneration: &responses.ToolImageGenerationParam{}, }}, }) if err != nil { panic(err) } for _, output := range response.Output { if output.Type != "image_generation_call" { continue } image, err := base64.StdEncoding.DecodeString(output.AsImageGenerationCall().Result) if err != nil { panic(err) } if err := os.WriteFile("cat_and_otter.png", image, 0o600); err != nil { panic(err) } return } panic("response did not include an image generation call")}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21require "base64"require "openai"client = OpenAI::Client.newresponse = client.responses.create( model: "gpt-5.6", input: "Generate an image of a gray tabby cat hugging an otter with an orange scarf.", tools: [{type: :image_generation}])image_call = response.output.find do |item| item.is_a?(OpenAI::Models::Responses::ResponseOutputItem::ImageGenerationCall)endunless image_call.is_a?(OpenAI::Models::Responses::ResponseOutputItem::ImageGenerationCall) raise "No image generation call returned"endFile.binwrite( "cat_and_otter.png", Base64.strict_decode64(image_call.result))
1
2
3
4
5
6
7
8openai responses create \ --model gpt-5.6 \ --raw-output \ --transform 'output.#(type=="image_generation_call").result' <<'YAML' | base64 --decode > cat_and_otter.pngtools: - type: image_generationinput: Generate an image of a gray tabby cat hugging an otter with an orange scarf.YAML
You can learn more about image generation in our Image
generation guide.
Using world knowledge for image generation
GPT Image models can use visual understanding of the world to generate lifelike images including real-life details without a reference.
For example, if you prompt GPT Image to generate an image of a glass cabinet with the most popular semi-precious stones, the model knows enough to select gemstones like amethyst, rose quartz, jade, etc, and depict them in a realistic way.
Analyze images
Vision is the ability for a model to “see” and understand images. If there is text in an image, the model can also understand the text.
It can understand most visual elements, including objects, shapes, colors, and textures, even if there are some limitations.
Giving a model images as input
You can provide images as input to generation requests either by providing a fully qualified URL to an image file, or providing an image as a Base64-encoded data URL.
You can provide multiple images as input in a single request by including multiple images in the content array, but keep in mind that images count as tokens and will be billed accordingly.