Skip to main content

Use DALL·E 2 in Python

Project description

Installation

pip install dalle2

Usage

Setup

  1. Go to https://openai.com/dall-e-2/
  2. Create a OpenAI Account
  3. Go to https://labs.openai.com/
  4. Open the Network Tab in Developer Tools
  5. Type a prompt and press "Generate"
  6. Look for fetch to https://labs.openai.com/api/labs/tasks
  7. In the request header look for authorization then get the Bearer Token
from dalle2 import Dalle2
dalle = Dalle2("sess-xxxxxxxxxxxxxxxxxxxxxxxxxxxx") # your bearer key

Generate images

generations = dalle.generate("portal to another dimension, digital art")
print(generations)
✔️ Task created with ID: task-xsuhOthvBXLEjddn3ynyiiOR
⌛ Waiting for task to finish...
...task not completed yet
...task not completed yet
...task not completed yet
...task not completed yet
🙌 Task completed!

[
  {
    'id': 'generation-sCnERSYDPP0Zu14fsdXEcKmL',
    'object': 'generation',
    'created': 1553332711,
    'generation_type': 'ImageGeneration',
    'generation': {
      'image_path': 'https://openailabsprodscus.blob.core.windows.net/private/user-hadpVzldsfs28CwvEZYMUT/generations/generation...'
    },
    'task_id': 'task-nERkiKsdjVCSZ50yD69qewID',
    'prompt_id': 'prompt-2CtaLQsgUbJHHDoJQy9Lul3T',
    'is_public': false
  },
  # 3 more ... 
]

Generate images and download them

file_paths = dalle.generate_and_download("portal to another dimension, digital art")
✔️ Task created with ID: task-xsuhOthvBXLEjddn3ynyiiOR
⌛ Waiting for task to finish...
...task not completed yet
...task not completed yet
...task not completed yet
...task not completed yet
🙌 Task completed!
✔️ Downloaded: C:\...\generation-XySidj4N8EN6Ok9ed15BZ2bs.webp
✔️ Downloaded: C:\...\generation-IK3UdxDz77FA5SLKpQPIITdU.webp
✔️ Downloaded: C:\...\generation-uNejKBXz1z6EQxJAT9pAZbof.webp
✔️ Downloaded: C:\...\generation-Ol1wEqNprf34vNohmJz0iUiE.webp

Generate a specific number of images

generations = dalle.generate_amount("portal to another dimension", 8) # Every generation has batch size 4 -> amount % 4 == 0 works best

# download images
file_paths = dalle.download(generations)
print(file_paths)
✔️ Task created with ID: task-lm0V4nZasgAFasd7AsStE67
⌛ Waiting for task to finish...
...task not completed yet
...task not completed yet
...task not completed yet
...task not completed yet
🙌 Task completed!
✔️ Task created with ID: task-WcetZOHt8asdvHb433gi
⌛ Waiting for task to finish...
...task not completed yet
...task not completed yet
...task not completed yet
...task not completed yet
🙌 Task completed!

Generate images from a masked file

DALL·E supports an "inpainting" API that fills-in transparent parts of an image. The website provides a tool to paint over an existing image to indicate which parts you want to be transparent. This Python package call assumes that the image you provide has already been processed to have transparent parts.

# make the right half of a saved image transparent
from PIL import Image, ImageDraw

image = Image.open('my_image.png')
m, n = image.size

area_to_keep = (0, 0, m//2, n)
image_alpha = Image.new("L", image.size, 0)
draw = ImageDraw.Draw(image_alpha)
draw.rectangle(area_to_keep, fill=255)

image_rgba = image.copy()
image_rgba.putalpha(image_alpha)
image_rgba = image_rgba.resize((1024, 1024)) # image must be square and 1024x1024
image_rgba.save('image_with_transparent_right_half.png')

# ask DALL·E to fill-in the transparent right half
generations = dalle.generate_from_masked_image(
    "portal to another dimension, digital art",
    "image_with_transparent_right_half.png",
)
✔️ Task created with ID: task-xsuhOthvBXLEjddn3ynyiiOR
⌛ Waiting for task to finish...
...task not completed yet
...task not completed yet
...task not completed yet
...task not completed yet
...task not completed yet
🙌 Task completed!

Other languages

Nodejs Package

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

dalle2-1.0.11.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

dalle2-1.0.11-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file dalle2-1.0.11.tar.gz.

File metadata

  • Download URL: dalle2-1.0.11.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.14

File hashes

Hashes for dalle2-1.0.11.tar.gz
Algorithm Hash digest
SHA256 2f372c700d961203f036267b524026e23eb79da32edd12c56af9c9e425c732cb
MD5 39d3994815d22a92312dfe5eaadb2924
BLAKE2b-256 f35fcbd2abcdd47b3c673949ff011fe92aa67622749c607e42292edae64c6734

See more details on using hashes here.

File details

Details for the file dalle2-1.0.11-py3-none-any.whl.

File metadata

  • Download URL: dalle2-1.0.11-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.14

File hashes

Hashes for dalle2-1.0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 3af9bf47345c9dc111dbcc881c2173393d3f5b721e8071879427ad8b72ec5076
MD5 22e89f287716d7352a2006d367b19357
BLAKE2b-256 58b3315f7a3586138805440b7377650e53bc38273e148a1248382de3bc2caf3a

See more details on using hashes here.

Supported by

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