Easy to use library for generating images using dall-e-3. Includes simple methods for inpainting, i.e. editing images using dall-e-2 and supports automatic mask generation when given 4 points representing a rectangle selection on top of the image you wish to inpaint over
Project description
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 # Replace with actual module filename
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"
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file noahs_image_generation-0.1.1.tar.gz.
File metadata
- Download URL: noahs_image_generation-0.1.1.tar.gz
- Upload date:
- Size: 6.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dab27f1b7976c52e7fda0e3a3365f09f1b8d505c9c2db077a5bd3b494a3ef13d
|
|
| MD5 |
dfa823b380b57a0e666b173fbd8fc8df
|
|
| BLAKE2b-256 |
5f8451b659e613b3cd0c639ee432c77eb63be3958001034d9e4ef21e1b6f1fd4
|
File details
Details for the file noahs_image_generation-0.1.1-py3-none-any.whl.
File metadata
- Download URL: noahs_image_generation-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4191ae1c352f0a4130e60df6da05daa22638c79bfbb6eaa8ae502cc6f071e99b
|
|
| MD5 |
b33e4ae7b928db0333140c2e37429a23
|
|
| BLAKE2b-256 |
eee96bfc4e5f0e9c3832969e7450387543cee0a19e855a458c656cb25289f23f
|