Skip to content

Add materials for "How to Generate Images With the OpenAI API" - #800

Open
realpython-bot wants to merge 1 commit into
realpython:masterfrom
realpython-bot:generate-images-openai
Open

Add materials for "How to Generate Images With the OpenAI API"#800
realpython-bot wants to merge 1 commit into
realpython:masterfrom
realpython-bot:generate-images-openai

Conversation

@realpython-bot

Copy link
Copy Markdown
Contributor

Sample code for the upcoming tutorial How to Generate Images With the OpenAI API (CMS post 2285).

This replaces the workflow in openai-dalle/, which no longer runs: OpenAI retired dall-e-2 and dall-e-3 on May 12, 2026 and removed the image variations endpoint at the same time. Calls to those models now return a 400, and /v1/images/variations returns a 404.

Contents

File Purpose
create_png.py Create an image from a text prompt and write it straight to a PNG
create.py Create an image and save the whole JSON response
convert.py Decode a Base64 JSON response into PNG files
edit.py Edit a saved image with a follow-up prompt

Notes for reviewers

  • All four scripts were run against the live API with gpt-image-2, not just adapted on paper.
  • create.py, convert.py, and edit.py are byte-identical to the corresponding code blocks in the tutorial.
  • convert.py carries over from openai-dalle/ unchanged — the response shape for saved JSON is the same.
  • edit.py replaces vary.py. The variations endpoint is gone, so editing with a prompt is the closest equivalent.
  • The GPT Image models always return Base64 data. There's no response_format parameter and no URL option, so the decode step is now required rather than optional.
  • images.edit() needs a (filename, bytes) tuple. Passing raw decoded bytes the way vary.py did fails with an unsupported-mimetype error, since the API infers the file type from the filename.

openai-dalle/ is left in place — say the word if you'd like it removed or marked deprecated in a follow-up.

🤖 Generated with Claude Code

Sample code for the tutorial on generating images with the OpenAI API
using the GPT Image models. Replaces the DALL-E workflow in
openai-dalle/, which no longer runs since OpenAI retired dall-e-2 and
dall-e-3 on May 12, 2026 along with the image variations endpoint.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant