Generate and Manipulate Images using OpenAI in Python

Welcome to the tutorial on using the OpenAI package in Python for image manipulation! OpenAI is a cutting-edge artificial intelligence (AI) research organization that has developed a wide range of powerful tools and libraries, including one specifically designed for image manipulation. In this tutorial, we will explore how you can leverage the OpenAI package to perform various image manipulation tasks, such as generating images, modifying images, and enhancing images, all using the power of AI. Whether you are a seasoned Python developer or just getting started with image manipulation, this tutorial will provide you with step-by-step instructions and examples to help you harness the capabilities of the OpenAI package and create stunning visual effects in your images. We will be going over the three ways you can use their package to edit photos, that is, creating new images, editing images, or creating image variations. So let's dive in and unlock the potential of OpenAI for image manipulation!

Step 1-) Create API Key

You need an API key to access their API. Initially, the API key is free. Keep this key secure.

https://platform.openai.com/account/api-keys

Step 2-) Pip Install OpenAI

In a shell, run the command:

> pip install openai

Step 3-) Image Generation

The first part of their package simply allows you to generate images from descriptions, which is quite remarkable and surprisingly pretty accurate. The code we use is as follows:

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)

The code does the following:

  • Imports the package and import constants (which contain our API Key). You can paste the secret in text in the code but that is bad practice.
  • We set the API key in the package and set the description for the AI
  • We call the API by passings in the params, where n is the number of images and size is the size of the image. As of writing the API only accepts a set of square images.
  • We get the URL from the API and open it.

The result I got looks as follows:

Pretty remarkable how it accurately matches the text description!

Note that the URL lasts a given amount of time (1 hour), so save it locally if you want to keep the image somewhere to show your friends or family.

Step 4-) Image Editing

You can also edit images with a mask using their package. This is my favorite part of the package because you can essentially photoshop a photo with an image, mask, and description. Here is the example I went over in the video.

Here is the mask I used:

You can see that the AI added the ball in the transparent area of the mask.

Some important things to note:

  • 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, the AI needs to have some context on the existing image, or else the edit will not work.
  • The image I found online was in “Palette” mode, so I needed to initially convert it to RGB by using the Pillow library in Python. You can uncomment the code below if you are having Palette issues.
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)

Step 5-) Image Variation

Finally, the simplest of the three is the image variation API they over. It simply allows you to pass an image and the AI creates a random variation in the image you provide. Here is an example.

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)

Pretty interesting the variation it gives us; it also adds some detail to the nails! The code is shown above.

Conclusion

Overall it is pretty easy to get started with this package and it can provide some powerful methods to manipulate images! Let me know what you think and how it helped you in your project. Interested to see the use cases of this in the coming months.

Create a free account to access full content.

All access to code and resources on ShillehTek.

Signup Now

Already a member? Sign In

Explore More on Our Blog

Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

Controlling an LED with a Snap Using the KY-037 Sound Sensor and Raspberry Pi

Discover how to set up, code, and activate the LED based on detected sound with the Raspberry Pi...

Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

Getting Started with the KY-037 Sound Sensor and Raspberry Pi: Detecting Sound Using Python

In this tutorial, I’ll guide you through setting up the KY-037 sound sensor with a Raspberry Pi using...

How to Post to Reddit Using Python

How to Post to Reddit Using Python

Post to reddit automatically using a Python script.

How to Create a Time-Lapse Video with a Raspberry Pi Camera

How to Create a Time-Lapse Video with a Raspberry Pi Camera

Learn how to make a timelapse with your Raspberry Pi in Python.

How to Integrate the MPU6050 with the STM32 Blue Pill

How to Integrate the MPU6050 with the STM32 Blue Pill

Learn how to measure acceleration with the STM32 and the MPU6050 in the Arduino IDE.

Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

Getting Started with STM32 Blue Pill in Arduino IDE Using a USB to TTL Converter — Write Your First Program

This comprehensive tutorial will guide you through the process of setting up and programming the STM32 Blue Pill...

Automate Task Scheduling in AWS with Lambda, Step Functions, and CloudWatch

Automate Task Scheduling in AWS with Lambda, Step Functions, and CloudWatch

In this tutorial, I'll show you how to automatically schedule tasks in AWS at regular intervals using AWS...

Implementing Google reCAPTCHA in a Simple React and Node.js App

Implementing Google reCAPTCHA in a Simple React and Node.js App

Learn how to protect your React applications from bots and spam with Google reCAPTCHA integration! This step-by-step tutorial...

AWS Lambda Tutorial: Using Selenium with Chromedriver in Python

AWS Lambda Tutorial: Using Selenium with Chromedriver in Python

In this tutorial, I will guide you through the process of running Selenium with ChromeDriver inside an AWS...

How to Connect MLX90614 Infrared Thermometer to Raspberry Pi Pico W: MicroPython Tutorial!

How to Connect MLX90614 Infrared Thermometer to Raspberry Pi Pico W: MicroPython Tutorial!

Learn how to use the MLX90614 with the Raspberry Pi Pico W and get infrared values in MicroPython.

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.