Skip to main content

Easy to use module 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

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.3.tar.gz (5.9 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.3-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: noahs_image_generation-0.1.3.tar.gz
  • Upload date:
  • Size: 5.9 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.3.tar.gz
Algorithm Hash digest
SHA256 9a66a7314d375d9a5e0e516ed14a9287148b36273e721e72b5d61266b46a6d09
MD5 09195697aa7fa0908aef016b840ca0c6
BLAKE2b-256 e7ecfa0e060f444d41c44d9f1a97a62d0d36e793c355ba95d34a3a42db08419e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for noahs_image_generation-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 182d6a122aa4458820acbec2c0977a7525b1ea3a3ec8ac6a3cc370b049bd6e1d
MD5 fbee108ad9c07f3d63272a096354d420
BLAKE2b-256 923c0abcaed9e129f886e65061737d73c6b8e73a3f1d73fc727a7f37719836b0

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