Video Tutorial (Optional)
Watch first if you want to follow along with the OpenAI image generation, image editing with a mask, and image variations in Python.
Project Overview
Python + OpenAI Image API: In this tutorial, you use the OpenAI Python package (Image API) to generate images from text, edit images using a mask, and create image variations to produce new outputs from an existing image.
You will walk through the three main workflows: creating new images, editing images, and creating image variations.
- Time: 15 to 30 minutes
- Skill level: Beginner to Intermediate
- What you will build: A Python script setup that can generate, edit (with a mask), and vary images using the OpenAI Image API
Parts List
From ShillehTek
- None required for this software-only tutorial.
External
- OpenAI API key - required to authenticate your requests
- Python environment - run the example scripts
- openai Python package - provides the Image API client
- (Optional) Pillow (PIL) - used if your image needs conversion to RGB (for example, if it is in “Palette” mode)
- Online PNG Tools mask editor - create a square transparency mask for image edits
Note: The Image API examples shown here use square images and square masks. Also, API-returned image URLs expire after a limited time (noted as about 1 hour in the original tutorial), so download and save images you want to keep.
Step-by-Step Guide
Step 1 - Create your OpenAI API key
Goal: Get an API key so your Python scripts can access the OpenAI API.
What to do: Create an API key at the link below. Keep your key secure.
https://platform.openai.com/account/api-keys
Expected result: You have an API key available to load into your code (for example, via a constants file or environment variable).
Step 2 - Install the OpenAI Python package
Goal: Install the dependency needed to call the Image API from Python.
What to do: In a shell, run:
pip install openai
Expected result: The openai package is installed and importable in your Python environment.
Step 3 - Generate an image from a text description
Goal: Create a brand new image from a prompt and retrieve the image URL returned by the API.
What to do: Run the following example. This version loads the API key from constants.API_KEY to avoid hardcoding secrets.
Code:
import openai
import constants
# Set up the OpenAI API client
openai.api_key = constants.API_KEY
description = "a stained glass window with an image of fruits"
# Generate a image from ChatGPT
response = openai.Image.create(prompt=description, n=1, size="512x512")
image_url = response["data"][0]["url"]
# Print the response
print(image_url)
Expected result: The script prints an image URL. Open the URL to see the generated image.
Step 4 - Edit an existing image using a mask
Goal: Modify part of an existing image by providing an input image, a transparency mask, and a prompt describing what to add in the masked area.
What to do: Prepare a square input image and a square mask. In the example below, the masked (transparent) area is where the AI will insert new content based on your prompt.
Important notes:
- You need a square image and a square mask.
- You can create a mask of an image here using this free online tool. It only allows a square mask.
- You need to describe the image itself and what you want in the mask, so the AI has context on the existing image.
- If the image is in “Palette” mode, convert it to RGB first (the Pillow snippet below is commented out in the example).
Code:
import openai
# from PIL import Image
import constants
# Set up the OpenAI API client
openai.api_key = constants.API_KEY
# Image.open("dalle-2 examples/cute_cat.png").convert("RGB").save(
# "dalle-2 examples/cute_cat.png"
# )
# Generate a image from ChatGPT
response = openai.Image.create_edit(
image=open("dalle-2 examples/cute_cat.png", "rb"),
mask=open("dalle-2 examples/mask.png", "rb"),
prompt="A black and white cat with a red ball",
n=1,
size="512x512",
)
image_url = response["data"][0]["url"]
# Print the response
print(image_url)
Expected result: The script prints an image URL where the masked region has been filled according to your prompt (for example, adding a ball in the transparent area).
Step 5 - Create image variations
Goal: Generate a random variation based on an input image you provide.
What to do: Provide an image file and call the variation API.
Code:
import openai
import constants
openai.api_key = constants.API_KEY
response = openai.Image.create_variation(
image=open("dalle-2 examples/cute_cat.png", "rb"),
n=1,
size="512x512",
)
image_url = response["data"][0]["url"]
# Print the response
print(image_url)
Expected result: The script prints an image URL that displays a new variation of your input image.
Conclusion
You now have a working Python workflow using the OpenAI Image API to generate images from text, edit images with a square mask, and create image variations. These three methods cover the core ways to create and manipulate images with the OpenAI package.
Want parts and tools for your next build? Shop at ShillehTek.com. If you want help turning an idea into a real project, integrating AI into a product, or customizing a solution for your setup, check out our IoT consulting services.