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

DALL-E Editor: Friendly Introduction for Developers

version 2024-04-16

My DALL-E Editor—a practical, feature-rich application built in Python 3.11+, 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.
  • Easily toggle HD mode specifically for DALL-E 3.
  • 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

70000 characters is a bit too much to post here now… You just get a docstring…

"""
dalle_editor_j.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. It supports generating 1024×1024 images
from textual prompts (images.generate), interactive mask drawing for precise editing,
and robust image editing capabilities (images.edit). Outputs can be seamlessly saved
to disk for integration into external workflows.

------------------------------------------------------------------------------
CURRENT FEATURES & USAGE PATTERNS
------------------------------------------------------------------------------
• **Image Generation**:
  - Supports OpenAI’s DALL·E 2, DALL·E 3, (GPT-4o?), and future image generation models.
  - 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.

• **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 1024×1024 workspace.
  - Persistent saving of edited images directly to disk for easy workflow integration.

• **User Interface Enhancements**:
  - Custom brush cursor visually indicates brush size and mode (draw/erase).
  - Contextual right-click menus in text fields offer standard editing operations
    (cut, copy, paste, select all) and placeholders for future AI-powered text rewriting.
  - Integrated visual progress indicators clearly communicate ongoing API interactions.

• **Model Flexibility & Future-Proofing**:
  - Direct integration with OpenAI's image generation and editing endpoints, assuming
    full functionality without fallback checks.
  - Modular code structure logically partitioned into minimally interdependent sections,
    facilitating straightforward future modularization and expansion.

------------------------------------------------------------------------------
MODULAR CODE STRUCTURE & PLANNED IMPROVEMENTS
------------------------------------------------------------------------------
• **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, enabling easy 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.

• **Scalable Virtual Canvas**:
  - Possible future support for a scalable "virtual canvas" beyond the current
    1024×1024 constraint, enabling expansive visual compositions.

• **Future Growth**:
  - Easily extendable with advanced features (e.g., layering, viewport panning, adjustable
    generation sizes) without requiring extensive refactoring.

------------------------------------------------------------------------------
DEVELOPER REQUIREMENTS & NOTES
------------------------------------------------------------------------------
• **Python 3.11+** required.
• **Install dependencies**:
  - Pillow:   `pip install pillow`
  - OpenAI:   `pip install openai`
• **OPENAI_API_KEY** environment variable must be set; otherwise, the application will
  prompt for an API key at startup.
• OpenAI's images.generate and images.edit methods are assumed fully functional; no
  fallback or additional validation is required. The code remains prepared to integrate
  newly introduced endpoints directly.
• Robust error handling and asynchronous requests (with a 40-second timeout) are implemented.


------------------------------------------------------------------------------
FUTURE DEV REFERENCE
------------------------------------------------------------------------------
One section particularly ripe for future growth is:

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

Significant complexity and advanced AI integration are anticipated here in subsequent
expansions.
"""

Download ZIP, link at upper right:

md5sum dalle_editor_j.zip
6164ecceb72f0a4f2d66e75513ec8804 *dalle_editor_j.zip

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…

2 Likes

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

There’s an easter egg in the first few lines of the code – did I guess how to send to gpt-4o correctly?