Creating Images in Python Using OpenAI's GPT Image API
Use Python and OpenAI's GPT Image API to create, edit, and save images, covering setup, keys, billing, size, quality, token costs, and Base64 output.
Key Takeaways
- OpenAI retired DALL·E 2 and DALL·E 3 on May 12, 2026, so Python image generation should use GPT Image models such as `gpt-image-2.5-flare`.
- Setup requires Python 3.10+, a virtual environment, the `openai` package, a secret API key, and a valid payment method before image calls can succeed.
- Images are generated with `client.images.generate()` and returned as Base64-encoded data in `.b64_json`, which must be decoded before saving as an image file.
- Existing images can be edited by passing them to `.edit()` with a new prompt, enabling a simple generate-save-edit workflow.
- Size and quality affect token cost, so developers should manage billing carefully, keep API keys private, and preserve full JSON responses for debugging.
Meta description: Learn how developers use Python to generate and edit images with OpenAI's GPT Image API, including setup, API keys, size, quality, token costs, and Base64.
Tags: OpenAI API, Python, image generation, GPT Image, Base64
Featured image prompt: An abstract, friendly illustration of code brackets and colorful geometric shapes, with no real people, no logos, and no text.
Developers who want to Generate Images in Python With OpenAI's GPT Image API can use Python code to create images from text prompts, save returned data, and edit existing pictures. The guide is useful because OpenAI retired DALL·E 2 and DALL·E 3 on May 12, 2026, and image work now runs through GPT Image models such as gpt-image-2.5-flare. It assumes basic experience with Python, JSON, and file operations.
What changed for OpenAI image tools
On May 12, 2026, OpenAI retired DALL·E 2 and DALL·E 3. It also removed the image variations endpoint at the same time. The GPT Image models introduced as their replacement power both chat-based image creation and the GPT Image API. A developer can send a text prompt from a Python script and receive image data instead of watching a chat window produce a picture in a few seconds.
The change matters for teams that already rely on OpenAI image tools. Scripts that called DALL·E endpoints need to move to GPT Image models. The tutorial points to a GPT Image model name, gpt-image-2.5-flare, as an example. The original guide also offers free sample code and an interactive quiz for readers who want to practice the workflow.
Image generation is a paid API service. OpenAI bills by tokens rather than by finished image, and the token amount used by a request depends on the model, size, and quality selected. Because API usage is tracked through unique key values, developers should keep their key private and check OpenAI's website for current pricing and policy details.
Set up Python and OpenAI access
- Confirm that Python version 3.10 or higher is available. The sample output in the source guide shows Python 3.14.6. In Windows PowerShell, run `python --version`. In Linux or macOS, run `python --version`.
- Create a virtual environment with `python -m venv venv`. This keeps project packages separate from the system Python installation.
- Activate the environment. On Windows PowerShell, use `.\venv\Scripts\activate`. On Linux or macOS, use `source venv/bin/activate`.
- Install the OpenAI Python library with `python -m pip install openai==3.13.0`. The package gives access to the full OpenAI API. For this guide, the focus is image generation, which uses GPT Image models to create and edit images from text prompts. The same package also supports text generation, and the source guide points readers to a separate article, How to Integrate ChatGPT's API With Python Projects.
- Sign up with OpenAI and open the API keys page in the account dashboard. Create a new secret key and copy the value shown. The key value is displayed only once, so store it somewhere safe before closing the window.
- Add a payment method and available credit on the billing page. An API key alone is not enough for image generation. If billing is not set up, the first call returns a quota or billing error instead of an image.
- Make the key available to Python by storing it in the environment variable `OPENAI_API_KEY`. In Windows PowerShell, use PowerShell environment-variable assignment syntax. In Linux or macOS, use shell export syntax. The key remains available in the current terminal session unless it is stored another way.
Generate and save image results
- Use the OpenAI client to call `client.images.generate()` with a text prompt and a GPT Image model such as `gpt-image-2.5-flare`. The prompt describes the picture you want. The `size` option controls image dimensions, and the `quality` option controls detail. These two settings also influence token cost.
- Read the JSON response. Image data arrives Base64-encoded in the `.b64_json` field. Base64 is a way to carry binary image bytes as text inside JSON. Decode that field with `b64decode()`, then write the resulting bytes to an image file.
- Keep the whole JSON response when possible. Saving only the decoded image can make later debugging harder. The full response preserves metadata and allows a script to decode several images at once if the API returns multiple images.
Edit images and manage costs
- To change an image you already have, pass that image back to `.edit()` with a new prompt. This turns the API into a small image editing pipeline: generate a starting picture, save it, then request edits from the saved file.
- Treat size and quality as product decisions, not just visual choices. A larger image or higher detail level may satisfy a design requirement, but it also changes the token cost of the request. Because OpenAI bills by tokens, a script that generates many images can spend credits faster than a developer expects.
- Keep API usage predictable by storing keys outside source code, checking billing status before running batch jobs, and reviewing the JSON response for each request. The source guide reminds readers that OpenAI API services and pricing policies may change, so current account settings matter.
Common mistakes
- Trying to use DALL·E 2 or DALL·E 3 after May 12, 2026. Those models were retired, and the image variations endpoint was removed.
- Creating an API key but not adding a payment method or available credit. Image generation is a paid service, so requests fail without billing support.
- Closing the terminal after setting `OPENAI_API_KEY` and assuming the key remains available. The example setup makes the key available only in the current terminal session.
- Saving only the `.b64_json` string instead of decoding it. The field contains Base64 text, not a directly viewable image file.
- Discarding the full JSON response. That response can include metadata and may contain several images, so saving it helps with debugging and batch decoding.
- Choosing `size` and `quality` without considering token costs. These settings affect image output and the amount billed.

Comments (0)