A FastAPI wrapper for OpenAI's image generation API that supports the new gpt-image-1 model as well as dall-e-2.
- Generate images using OpenAI's
gpt-image-1model - Edit images with multiple source images (supported by
gpt-image-1anddall-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
- Python 3.8+
- OpenAI API key
-
Clone this repository:
git clone <repository-url> cd openai-image-generator -
Install dependencies:
pip install -r requirements.txt -
Create a
.envfile from the example:cp .env.example .env -
Add your OpenAI API key to the
.envfile:OPENAI_API_KEY=your_openai_api_key_here
-
Start the server:
python main.pyAlternatively, you can use uvicorn directly:
uvicorn main:app --reload -
Access the API documentation at
http://localhost:8000/docs
POST /images/generations
Parameters:
prompt(string, required): A text description of the desired imagemodel(string, default: "gpt-image-1"): The model to use for image generationn(integer, default: 1): The number of images to generatesize(string, default: "1024x1024"): The size of the generated images ("1024x1024", "1792x1024", or "1024x1792")
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 imagemodel(string, default: "gpt-image-1"): The model to use for image editingn(integer, default: 1): The number of images to generatesize(string, default: "1024x1024"): The size of the generated images
POST /images/variations
Parameters:
image(file, required): The image to create variations ofmodel(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 generatesize(string, default: "1024x1024"): The size of the generated images
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'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)- Image generation
- Image editing with multiple source images
- Supports sizes: 1024x1024, 1792x1024, 1024x1792
- Image generation
- Image editing (single source image)
- Image variations
- Supports sizes: 256x256, 512x512, 1024x1024
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.
MIT