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.2.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.2-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: noahs_image_generation-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 3cf41e3c779a8cf640b4cec4319750f6d1a1dd9bc9159ac02d722e5a4402aa90
MD5 afd41f62b083fafc0ae497c036194b2c
BLAKE2b-256 2855f3e70cd2184d7456081924f89be77c4d54354f99a8edf035da28ade5dd69

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for noahs_image_generation-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 916a497a1f4d3fa0a1232d82962af14ee9687bc98308d2702c7410935a30f31e
MD5 78d45368df85f1be5d2c4cda23cd038e
BLAKE2b-256 bc03785fbf48b2e5464eed2f673d77aeea0e8b6e5632dbcc425e10761d6fe40c

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