forked from realpython/materials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
87758dd
commit fbcd91f
Showing
7 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Generate Images With DALL·E 2 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 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 -m venv venv | ||
$ source venv/bin/activate | ||
(venv) $ python -m pip install openai | ||
``` | ||
|
||
You need to be on Python 3.7.1 or higher. | ||
|
||
## Create Images and Image Variations | ||
|
||
Follow the instructions in [the tutorial](https://realpython.com/generate-images-with-dalle-openai-api/) to create images from text prompts, create image variations, and convert a Base64 JSON response to a PNG image file. | ||
|
||
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. | ||
- `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. | ||
|
||
The tutorial walks you 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. | ||
|
||
You can run `edit.py` to give this functionality a try. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import json | ||
from base64 import b64decode | ||
from pathlib import Path | ||
|
||
JSON_FILE_NAME = "An ec-1667994848" | ||
IMAGE_DIR = Path.cwd() / "images" / JSON_FILE_NAME | ||
DATA_DIR = Path.cwd() / "responses" | ||
|
||
IMAGE_DIR.mkdir(parents=True, exist_ok=True) | ||
|
||
with open(DATA_DIR / f"{JSON_FILE_NAME}.json", "r") as f: | ||
response = json.load(f) | ||
|
||
for index, image_dict in enumerate(response["data"]): | ||
image_data = b64decode(image_dict["b64_json"]) | ||
with open(IMAGE_DIR / f"{JSON_FILE_NAME}-{index}.png", "wb") as f: | ||
f.write(image_data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import json | ||
import os | ||
from pathlib import Path | ||
|
||
import 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( | ||
prompt=PROMPT, | ||
n=1, | ||
size="256x256", | ||
response_format="b64_json", | ||
) | ||
|
||
file_name = DATA_DIR / f"{PROMPT[:5]}-{response['created']}.json" | ||
|
||
with open(file_name, mode="w") as file_path: | ||
json.dump(response, file_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import json | ||
import os | ||
from pathlib import Path | ||
|
||
import openai | ||
|
||
SOURCE_PATH = Path.cwd() / "images" / "An ec-1667994848" | ||
DESTINATION_PATH = Path.cwd() / "responses" | ||
PROMPT = "A 90s vaporwave computer showing Rick Astley on the screen" | ||
|
||
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( | ||
image=open(SOURCE_PATH / "computer.png", "rb"), | ||
mask=open(SOURCE_PATH / "mask.png", "rb"), | ||
prompt=PROMPT, | ||
n=1, | ||
size="256x256", | ||
response_format="b64_json", | ||
) | ||
|
||
with open( | ||
DESTINATION_PATH / f"edit-{PROMPT[:5]}-{response['created']}.json", | ||
mode="w", | ||
) as fp: | ||
json.dump(response, fp) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import json | ||
import os | ||
from base64 import b64decode | ||
from pathlib import Path | ||
|
||
import openai | ||
|
||
SOURCE_FILE = "A com-1667994848.json" | ||
DATA_DIR = Path.cwd() / "responses" | ||
|
||
openai.api_key = os.getenv("OPENAI_API_KEY") | ||
|
||
with open(DATA_DIR / SOURCE_FILE, "r") as json_file: | ||
saved_response = json.load(json_file) | ||
image_data = b64decode(saved_response["data"][0]["b64_json"]) | ||
|
||
response = openai.Image.create_variation( | ||
image=image_data, | ||
n=3, | ||
size="256x256", | ||
response_format="b64_json", | ||
) | ||
|
||
new_file_name = f"vary-{SOURCE_FILE[:4]}-{response['created']}.json" | ||
|
||
with open(DATA_DIR / new_file_name, mode="w") as new_json_file: | ||
json.dump(response, new_json_file) |