DALL-E 2/3 create, DALL-E 2 edit tool in Python for you

DALL-E Editor: an OpenAI DALL-E app to draw and receive mask edits

version 2024-05-17 - preliminary gpt-images-1

My DALL-E Editor—a practical, feature-rich application built in Python 3.10+, designed specifically to streamline interactions with OpenAI’s DALL-E 2 and DALL-E 3 APIs. This user-friendly GUI application leverages Python’s Tkinter library to simplify image generation, editing, and masking workflows, allowing quick experimentation with AI-generated visuals directly from your desktop.

The main focus is the edits endpoint for redraw and infill, at $0.02 per image, thus, this has a 1024x1024 image only at this point in development

Key Features and Capabilities:

1. Image Generation from Text Prompts

  • Instantly generate high-quality images (1024×1024 pixels) using your descriptive text prompts.
  • Choose between DALL-E 2 and DALL-E 3 models effortlessly. gpt-image-1 also there if you ID validate.
  • Easily toggle HD mode specifically for DALL-E 3 (or try it’s poorer “natural” style).
  • An AI placeholder - develop your own AI calls for prompt improvement, or even prompt library dialog or style (or character) selector.

2. Intuitive Mask Drawing Interface

  • Draw precise masks directly on images for targeted editing or inpainting.
  • Selectable brush sizes (Small, Medium, Large) to customize mask precision.
  • Visual feedback through a semi-transparent red overlay, clearly showing masked regions.

3. Dynamic Image Loading and Scaling

  • Load images directly from your file system.
  • Interactive slider for dynamically scaling images—choose between cropping to fit or filling (outfill) with optional shrinking allowed, and dragging around.
  • Preview adjustments in real-time, allowing precise control of how images fit the target resolution.
  • Image transparency is preserved and used for mask also (observe the blue actual size)

4. Integrated Editing Workflow

  • Send masked images along with editing prompts to DALL-E’s editing API endpoints seamlessly.
  • Smart default prompts when performing outfill operations, simplifying repetitive editing tasks - or what you type

5. Robust Error Handling

  • Automatically manages API errors gracefully, ensuring your workflow remains uninterrupted and clear error dialogs inform you promptly.

6. Convenient File Operations

  • Easily save your generated and edited images to disk as standard PNG files.

Requirements:

  • Python 3.11+
  • Pillow: pip install pillow
  • OpenAI Python SDK: pip install openai
  • Funding your OpenAI account with credits to burn
  • Set the environment variable OPENAI_API_KEY with your API key.

The code

Too large for the forum… You just get a docstring…

#!/usr/bin/env python3
"""
dalle_editor.py

A developer-oriented, single-file Tkinter GUI application leveraging OpenAI's DALL·E
API endpoints to create, edit, and refine images via generative AI techniques, including
inpainting, outpainting, and generative fills. Supports:
- loading input files at varying crops or zooms, at canvas position.
- generating 1024x1024 images from textual prompts,
- interactive mask drawing for precise editing,
- robust image editing capabilities.
- copying canvas with transparency, saving to file

------------------------------------------------------------------------------
REQUIRES A TYPICAL OPENAI DEVELOPER ENVIRONMENT ON A DESKTOP OS
------------------------------------------------------------------------------
- Python 3.10+
- dependencies:
  - Pillow:   `pip install pillow`
  - OpenAI:   `pip install openai`
- OPENAI_API_KEY environment variable must be set; otherwise,
    the application will prompt for a temp API key at startup.

------------------------------------------------------------------------------
CURRENT FEATURES & USAGE PATTERNS
------------------------------------------------------------------------------
- Image Generation:
  - Supports OpenAI dall-e-2, dall-e-3, gpt-image-1.
  - Dynamically manages advanced parameters (e.g., "HD," "Natural") for DALL·E 3.
  - Provides real-time character counters and silent prompt truncation to adhere to
    model-specific prompt length constraints.

- gpt-image-1:
  - Model does not perform edit without altering and regenerating the whole image.
  - Preliminary image + mask support is surfaced, not multiple image re-synthesis.
  - No "quality" or "transparency" UI options; only supported on generate

- Mask Drawing & Editing:
  - Interactive brush tool enables intuitive mask painting directly onto images, stored
    as metadata in a Pillow "L" mode image. A semi-transparent red overlay provides
    immediate visual feedback.
  - Utilizes OpenAI's images.edit endpoint for precise inpainting and outpainting tasks,
    with robust error handling to revert to the last stable image state upon failures.

- Image Import & Export:
  - Flexible image loading from disk with dynamic resizing options ("Allow shrink,"
    "Allow zoom") to adapt external images into the standardized 1024x1024 workspace.
  - Persistent saving of edited images directly to disk for easy workflow integration.

- User Interface:
  - Brush size cursor visually indicates mask draw area; mode by its color.
  - Contextual right-click menus in text fields offer standard editing operations
    (cut, copy, paste, select all).
  - Progress indication while making API calls.

- Modular Style:
  - Although presented as a single file for clarity, the application is logically
    partitioned into distinct sections (API wrapper, dialogs, main editor), each with
    minimal global dependencies, future modularization.
  - `DalleAPI`: Asynchronous API client for OpenAI's images endpoints.
  - `ImageLoadDialog`: Manages image importing and resizing, returning processed images
    and masks.
  - `DalleEditorApp`: Main GUI class orchestrating UI elements, event handling, and data
    flow, supplemented by external utility functions.

------------------------------------------------------------------------------
A ROBUST APPLICATION WOULD OFFER "labs.openai.com" UI and beyond
------------------------------------------------------------------------------

- Larger Scalable Virtual Canvas:
  - Images beyond the current 1024x1024 constraint.
  - layering, viewport panning, generation and placement, and sectional outfill
- Some "undo" beyond resetting masks

------------------------------------------------------------------------------
AI-Powered Prompting framework for you
------------------------------------------------------------------------------
    def ai_rewrite_text(self):
        '''a placeholder for an extensive AI-powered function'''

This function is in the right-click menu for both UI prompts.
It will demonstrate readiness by placing a selection within asterisks.
You could develop your own AI language improvers or even dialogs.
"""

Download link

dalle_editor_2025-05-17.py

md5sum dalle_editor_2025-05-17.py
a36a27703b154718e7aa3af5c61838cd *dalle_editor_2025-05-17.py

Explore, integrate, and extend freely—DALL-E Editor is designed for developers to adapt, improve, and seamlessly integrate advanced image-generation capabilities directly into their Python applications. If I get around to it - the features of OpenAI’s labs - adding multiple images to a virtual canvas…

3 Likes

I’ve finally stopped the feature creep for a while, and it’s time to share this with the community!

May 17 - I’ve added basic gpt-images-1 buttons (the gpt-4o based model) and API parameters. Only have run this up to receiving the “you must validate ID” API error, and verifying the sent API calls, so please report back.

So cool! How do I start using this? I’m a coding beginner. Thank you!

It is an application, ready to go, except that you need a local Python execution environment on a GUI desktop operating system, and a display at least 1080p to see the 1:1 1024x1024 image presented. Download Python | Python.org -

If you are already running OpenAI scripts, you likely just need to install PIL (Pillow) for image processing by Python.

The zip file has a single Python file to extract, save somewhere, and run (even just a double-click on the file).
Provides: DALL-E 2 for generation and mask fill; DALL-E 3 for generation by prompt.


DEVELOPER REQUIREMENTS & NOTES

  • Python 3.10+ required (because of my use of built-in type annotations).
  • Install dependencies:
    • Pillow: pip install pillow
    • OpenAI: pip install openai
  • OPENAI_API_KEY environment variable must be set; otherwise, the application will prompt for a temporary API key at startup.
  • Your funded OpenAI account that can already satisfy API calls.

When I download the file linked above, “Dalle_editor_j.zip - OpenDrive”, it saves in my downloads as a “.py” file, and when I click on it, two windows open: first a window displaying all the code, and second a new terminal. Where do I go from there? Thank you for your help!

You can follow OpenAI’s own “quickstart” for Python, the “Documentation” link in the sidebar here.

You’ll need to actually extract the downloaded python file from the zip to a location somewhere. Operating systems have a bad habit of showing the contents of a “zip” like a folder.

Try dragging the dalle_editor_j.py file to your desktop from the “directory” where you see the download (or use 7zip for very clear compressed file extraction). Or right-click-> extract. or similar, depending on the OS.

If you don’t want to see an extra shell window for diagnosis, you can change the file extension from .py to .pyw.

Fully-installed Python gives the IDLE editor, with its own “run” command:

It sounds like the missing window could be the actual user interface, which uses Python’s built-in tkinter UI. An option that you could opt-out of installing.

Or - it could be that your OS is opening an editor instead of launching, if you don’t have the quick-launch helper for Python with Python in your path.

You can be more deliberate about launching it: at a shell:
python my_python_file.py

(Or you can see a terrible OS foisting itself on you to give an unknown install if you use the legacy python3 command instead of merely python which is successful)

Chatting with a Prompt Playground AI model about your situation may answer your specific questions, given the dozens of possible ways Python execution may be available to you or be installed.

Good luck! Having a development environment working and ready to run API reference examples locally will get you thinking about your own ways to use AI APIs.

Thank you so much! I got the program to open by clicking ‘open with Python Launcher!’

Funnily enough, when I ran my first prompt, it gave me a 500 bad request error! This has been my experience over the past week trying to run Dall-e-2 in python. I’m not sure if you recall but you were helping me answer questions over in another community post about this error. I’m still not sure what the issue is, but it feels especially problematic now that I’m using your edit tool and still getting the same error.

Any thoughts? Thanks again. Can’t wait to get this solved!

By the way, the image did load when I selected Dall-e-3. No error! I just want to use Dall-e-2, though…

I just ran many calls, and got several “400” errors without error message contents from the API, apparently based on the (relatively innocent) prompt language or length. A status 500 error was not implicated in my tests, and that error thus seems to be something following your account itself.

I would try what you can do yourself to affect things:

  • Generate a new project in your platform account, (maybe named “images”)
  • Generate a new API key for it
  • Do not set limits or budget on the project
  • Review your environment variables, ensuring you have placed that OPENAI_API_KEY and do not have other OPENAI_ORG_ID or OPENAI_PROJECT_ID environment variables being picked up from anywhere.

That has a low chance of working, but there is a chance.

You can then message “help” through the platform site, informing of you account-specific API service issues needing action – for you are using an application that has been tested by others dozens and dozens of times to work using the OpenAI SDK library, not sending bad requests.


If wanting wide or tall images generated from DALL-E 3, for now, you’ll need to move to your own requests instead of this little utility focusing on edits.


gpt-image-1…

1 Like