Skip to content

Commit

Permalink
Merge pull request #570 from realpython/update-dalle
Browse files Browse the repository at this point in the history
Update code for new API and model versions
  • Loading branch information
brendaweles authored Aug 20, 2024
2 parents 05b444e + 8aa59fa commit fb0bb74
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 42 deletions.
11 changes: 6 additions & 5 deletions openai-dalle/README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# Generate Images With DALL·E 2 and the OpenAI API
# Generate Images With DALL·E and the OpenAI API

Learn to use the OpenAI Python library to create images with DALL·E, a state-of-the-art latent diffusion model. In the associated tutorial on [generating images with DALL·E 2 and the OpenAI API](https://realpython.com/generate-images-with-dalle-openai-api/), you explore image creation and generating image variations. You learn how to interact with DALL·E using API calls and incorporate this functionality into your Python scripts.
Learn to use the OpenAI Python library to create images with DALL·E, a state-of-the-art latent diffusion model. In the associated tutorial on [generating images with DALL·E and the OpenAI API](https://realpython.com/generate-images-with-dalle-openai-api/), you'll explore image creation and generating image variations. You'll learn how to interact with DALL·E using API calls and incorporate this functionality into your Python scripts.

## Setup

Create and activate a virtual environment, then install the `openai` package:

```console
$ python --version
Python 3.11.0
Python 3.12.5
$ python -m venv venv
$ source venv/bin/activate
(venv) $ python -m pip install openai
Expand All @@ -22,14 +22,15 @@ Follow the instructions in [the tutorial](https://realpython.com/generate-images

You can find the code for each of these steps in dedicated scripts:

- `create.py`: Create an image from a text prompt and save the image data to a file.
- `create_dalle3.py`: Create an image from a text prompt using DALL·E 3 and save the image data to a file.
- `create.py`: Create an image from a text prompt using DALL·E 2 and save the image data to a file.
- `convert.py`: Convert a Base64-encoded PNG image delivered in a JSON response to a PNG image file.
- `vary.py`: Read Base64-encoded image data and make an API request to receive variations of that image.

In the tutorial, you'll walk through each of these scripts and their functionality and output in more detail.

## Edit Images (Inpainting and Outpainting)

The OpenAI Image API also allows you to [edit parts of an image](https://beta.openai.com/docs/guides/images/edits) using text prompts. For this, you need to create a mask with transparent image data in the area where you want to edit the image.
The OpenAI Image API also allows you to [edit parts of an image](https://platform.openai.com/docs/guides/images/edits-dall-e-2-only) using text prompts. For this, you need to create a mask with transparent image data in the area where you want to edit the image.

You can run `edit.py` to give this functionality a try.
14 changes: 7 additions & 7 deletions openai-dalle/create.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import json
import os
from pathlib import Path

import openai
from openai import OpenAI

client = OpenAI()

PROMPT = "An eco-friendly computer from the 90s in the style of vaporwave"
DATA_DIR = Path.cwd() / "responses"

DATA_DIR.mkdir(exist_ok=True)

openai.api_key = os.getenv("OPENAI_API_KEY")

response = openai.Image.create(
response = client.images.generate(
model="dall-e-2",
prompt=PROMPT,
n=1,
size="256x256",
response_format="b64_json",
)

file_name = DATA_DIR / f"{PROMPT[:5]}-{response['created']}.json"
file_name = DATA_DIR / f"{PROMPT[:5]}-{response.created}.json"

with open(file_name, mode="w", encoding="utf-8") as file:
json.dump(response, file)
json.dump(response.to_dict(), file)
14 changes: 14 additions & 0 deletions openai-dalle/create_dalle3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from openai import OpenAI

client = OpenAI()

PROMPT = "A vaporwave computer"


response = client.images.generate(
model="dall-e-3",
prompt=PROMPT,
)

print(response.data[0].url)
print(response.data[0].revised_prompt)
13 changes: 6 additions & 7 deletions openai-dalle/edit.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import json
import os
from pathlib import Path

import openai
from openai import OpenAI

client = OpenAI()

SOURCE_PATH = Path.cwd() / "images" / "An ec-1667994848"
DESTINATION_PATH = Path.cwd() / "responses"
Expand All @@ -11,9 +12,7 @@
SOURCE_PATH.mkdir(parents=True, exist_ok=True)
DESTINATION_PATH.mkdir(parents=True, exist_ok=True)

openai.api_key = os.getenv("OPENAI_API_KEY")

response = openai.Image.create_edit(
response = client.images.edit(
image=open(SOURCE_PATH / "computer.png", mode="rb"),
mask=open(SOURCE_PATH / "mask.png", mode="rb"),
prompt=PROMPT,
Expand All @@ -23,8 +22,8 @@
)

with open(
DESTINATION_PATH / f"edit-{PROMPT[:5]}-{response['created']}.json",
DESTINATION_PATH / f"edit-{PROMPT[:5]}-{response.created}.json",
mode="w",
encoding="utf-8",
) as file:
json.dump(response, file)
json.dump(response.to_dict(), file)
31 changes: 15 additions & 16 deletions openai-dalle/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
certifi==2022.9.24
charset-normalizer==2.1.1
et-xmlfile==1.1.0
idna==3.4
numpy==1.23.4
openai==0.25.0
openpyxl==3.0.10
pandas==1.5.1
pandas-stubs==1.2.0.62
python-dateutil==2.8.2
pytz==2022.6
requests==2.28.1
six==1.16.0
tqdm==4.64.1
typing_extensions==4.4.0
urllib3==1.26.12
annotated-types==0.7.0
anyio==4.4.0
certifi==2024.7.4
distro==1.9.0
h11==0.14.0
httpcore==1.0.5
httpx==0.27.0
idna==3.7
jiter==0.5.0
openai==1.40.6
pydantic==2.8.2
pydantic_core==2.20.1
sniffio==1.3.1
tqdm==4.66.5
typing_extensions==4.12.2
13 changes: 6 additions & 7 deletions openai-dalle/vary.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import json
import os
from base64 import b64decode
from pathlib import Path

import openai
from openai import OpenAI

client = OpenAI()

DATA_DIR = Path.cwd() / "responses"
SOURCE_FILE = DATA_DIR / "An ec-1667994848.json"

openai.api_key = os.getenv("OPENAI_API_KEY")

with open(SOURCE_FILE, mode="r", encoding="utf-8") as json_file:
saved_response = json.load(json_file)
image_data = b64decode(saved_response["data"][0]["b64_json"])

response = openai.Image.create_variation(
response = client.images.create_variation(
image=image_data,
n=3,
size="256x256",
response_format="b64_json",
)

new_file_name = f"vary-{SOURCE_FILE.stem[:5]}-{response['created']}.json"
new_file_name = f"vary-{SOURCE_FILE.stem[:5]}-{response.created}.json"

with open(DATA_DIR / new_file_name, mode="w", encoding="utf-8") as file:
json.dump(response, file)
json.dump(response.to_dict(), file)

0 comments on commit fb0bb74

Please sign in to comment.