Skip to content

A FastAPI wrapper for OpenAI's image generation API that supports the new `gpt-image-1` model as well as `dall-e-2`.

Notifications You must be signed in to change notification settings

terrymccann/openai-image

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenAI Image Generator API

A FastAPI wrapper for OpenAI's image generation API that supports the new gpt-image-1 model as well as dall-e-2.

Features

  • Generate images using OpenAI's gpt-image-1 model
  • Edit images with multiple source images (supported by gpt-image-1 and dall-e-2)
  • Create image variations (only supported by dall-e-2)
  • Compatible with OpenAI's API parameter constraints
  • Simple REST API interface with form data support for file uploads
  • User-friendly web interface for all operations

Requirements

  • Python 3.8+
  • OpenAI API key

Installation

  1. Clone this repository:

    git clone <repository-url>
    cd openai-image-generator
    
  2. Install dependencies:

    pip install -r requirements.txt
    
  3. Create a .env file from the example:

    cp .env.example .env
    
  4. Add your OpenAI API key to the .env file:

    OPENAI_API_KEY=your_openai_api_key_here
    

Usage

  1. Start the server:

    python main.py
    

    Alternatively, you can use uvicorn directly:

    uvicorn main:app --reload
    
  2. Access the API documentation at http://localhost:8000/docs

API Endpoints

Generate Image

POST /images/generations

Parameters:

  • prompt (string, required): A text description of the desired image
  • model (string, default: "gpt-image-1"): The model to use for image generation
  • n (integer, default: 1): The number of images to generate
  • size (string, default: "1024x1024"): The size of the generated images ("1024x1024", "1792x1024", or "1024x1792")

Edit Image

POST /images/edits

Parameters:

  • image (file(s), required): The image(s) to edit (multiple files allowed)
  • prompt (string, required): A text description of the desired edited image
  • model (string, default: "gpt-image-1"): The model to use for image editing
  • n (integer, default: 1): The number of images to generate
  • size (string, default: "1024x1024"): The size of the generated images

Create Image Variation

POST /images/variations

Parameters:

  • image (file, required): The image to create variations of
  • model (string, default: "dall-e-2"): The model to use (only "dall-e-2" is supported for variations)
  • n (integer, default: 1): The number of variations to generate
  • size (string, default: "1024x1024"): The size of the generated images

Examples

Using cURL

Generate an image with gpt-image-1:

curl -X 'POST' \
  'http://localhost:8000/images/generations' \
  -H 'accept: application/json' \
  -F 'prompt=A cute baby sea otter' \
  -F 'model=gpt-image-1' \
  -F 'n=1' \
  -F 'size=1024x1024'

Edit multiple images:

curl -X 'POST' \
  'http://localhost:8000/images/edits' \
  -H 'accept: application/json' \
  -F 'image=@body-lotion.png' \
  -F 'image=@bath-bomb.png' \
  -F 'image=@incense-kit.png' \
  -F 'image=@soap.png' \
  -F 'prompt=Create a lovely gift basket with these four items in it' \
  -F 'model=gpt-image-1'

Using Python with requests

import requests
import json
import base64

# Generate image
url = "http://localhost:8000/images/generations"
payload = {
    "prompt": "A cute baby sea otter",
    "model": "gpt-image-1",
    "n": 1,
    "size": "1024x1024"
}
response = requests.post(url, data=payload)
result = response.json()

# Save the image
with open("otter.png", "wb") as f:
    image_data = base64.b64decode(result["data"][0]["b64_json"])
    f.write(image_data)

Model Capabilities

gpt-image-1

  • Image generation
  • Image editing with multiple source images
  • Supports sizes: 1024x1024, 1792x1024, 1024x1792

dall-e-2

  • Image generation
  • Image editing (single source image)
  • Image variations
  • Supports sizes: 256x256, 512x512, 1024x1024

Additional Notes

This application is designed to be compatible with the current version of the OpenAI API. OpenAI's API documentation mentions parameters like quality, style, and response_format, but these may not be supported in all versions of the API. If you encounter errors about unsupported parameters, you may need to update the OpenAI Python package.

License

MIT

About

A FastAPI wrapper for OpenAI's image generation API that supports the new `gpt-image-1` model as well as `dall-e-2`.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published