Skip to main content

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

noahs_image_generation-0.1.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

noahs_image_generation-0.1.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file noahs_image_generation-0.1.0.tar.gz.

File metadata

  • Download URL: noahs_image_generation-0.1.0.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

Hashes for noahs_image_generation-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0444cc5c253d0211bab5e3ac4f1e3e450d64e431c05d97fa873de0e5d92f2534
MD5 f1a569093e6ea9c481ccd179e93596b7
BLAKE2b-256 44e3aa7098ffbeec4cd17afafe51f33278ab27ea57308c015a7c2afa646af544

See more details on using hashes here.

File details

Details for the file noahs_image_generation-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for noahs_image_generation-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9fe608f03851789f8d86dbca4d41513a2f2a98f8d5eb7d924524822dcd7dd631
MD5 71b574f68451837677e8ff5f7505cd92
BLAKE2b-256 ae4877448dc6c4720d703c4a97b041ca50f447ec5b57f54a697be4fd8a4e987b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page