Image Inpainting & Generation with OpenAI DALL·E
This module provides a convenient Python interface for generating and editing images using OpenAI's DALL·E models. It supports:
- Generating new images from text prompts
- Inpainting selected regions of an existing image
- Automatic resizing/padding/stretching of images and masks
- Flask support for web-based image editing
Requirements
pip install noahs_image_generation
Make sure your environment variable OPENAI_API_KEY is set, or pass your key directly into the class.
Features
- Resize and pad images while maintaining aspect ratio
- Stretch images to exact dimensions
- Generate transparent masks from selected polygon regions
- Inpaint images with DALL·E 2
- Generate new images with DALL·E 3
- In-memory and file-based support
Usage
Initialization
from noahs_image_generation import ImageGenerator
generator = ImageGenerator(api_key="your-openai-api-key")
Generate an Image In-Memory
prompt = "a cat astronaut riding a bicycle on Mars"
image_bytes = generator.generate_image_in_memory(prompt)
# Save or display the image
with open("cat_astronaut.png", "wb") as f:
f.write(image_bytes.read())
Inpaint an Image with a Mask (From File)
output_bytes, mimetype = generator.inpaint(
image_input="frog.png",
coordinates=[(420, 25), (620, 25), (620, 300), (420, 300)], # rectangle selection on frog.png where you want the hat
prompt="Give the frog a cowboy hat"
)
# Save the inpainted image
with open("frog_with_hat.png", "wb") as f:
f.write(output_bytes.read())
Inpaint an Image (From Bytes)
# Read image into bytes
with open("frog.png", "rb") as f:
image_bytes = f.read()
output_bytes, mimetype = generator.inpaint(
image_input=image_bytes,
coordinates=[(420, 25), (620, 25), (620, 300), (420, 300)],
prompt="Give the frog a cowboy hat"
)
with open("frog_with_hat_from_bytes.png", "wb") as f:
f.write(output_bytes.read())
Inpaint an Image from a Flask Request
# Assuming you are in a Flask route
@app.route("/inpaint", methods=["POST"])
def inpaint_route():
generator = ImageGenerator()
output_bytes, mimetype = generator.inpaint(flask_request=request)
return send_file(output_bytes, mimetype=mimetype)
curl example to call the Flask endpoint:
curl -X POST http://localhost:5000/inpaint \
-F "image=@my_image.png" \
-F "coordinates=[[100,100],[200,100],[200,200],[100,200]]" \
-F "prompt=draw a dragon tattoo on the arm"
Release files for noahs-image-generation 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| noahs_image_generation-0.1.3.tar.gz | 5.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| noahs_image_generation-0.1.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.3 kB
Release files / noahs_image_generation-0.1.3.tar.gz
| Download URL | noahs_image_generation-0.1.3.tar.gz |
|---|---|
| Size | 5.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9a66a7314d375d9a5e0e516ed14a9287148b36273e721e72b5d61266b46a6d09
|
|
BLAKE2b-256 checksum How to use checksums |
e7ecfa0e060f444d41c44d9f1a97a62d0d36e793c355ba95d34a3a42db08419e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.4
|
Release files / noahs_image_generation-0.1.3-py3-none-any.whl
| Download URL | noahs_image_generation-0.1.3-py3-none-any.whl |
|---|---|
| Size | 6.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
182d6a122aa4458820acbec2c0977a7525b1ea3a3ec8ac6a3cc370b049bd6e1d
|
|
BLAKE2b-256 checksum How to use checksums |
923c0abcaed9e129f886e65061737d73c6b8e73a3f1d73fc727a7f37719836b0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.11.4
|