Using Open AI’s image generator
This tutorial explains how to use Open AI’s image generator to create stunning images using Visual Studio Code (VSCode) and Python. I’ll walk you through the process, including how to set up a Python environment, install OpenAI, generate an API key, and use the image generation script. With a few simple steps, you can be up and running in no time, creating and manipulating images with the power of Open AI’s image generator and Python. Have fun and get creative!
Start here if you do not have a Python environment set up already.
For this tutorial, I’ll walk you through the process I completed on my MacBook with VSCode. There are dozens of other Python environments that you can use for this, and of course, you can use your Windows machine (as much as my inner Apple fan-boy doesn’t want you to). Be sure to read the pages linked and follow the instructions carefully as not every code sample can be copied and pasted directly into your terminal to get everything installed.
Once you’ve installed VSCode and set-up your PATH, open a new terminal window in VSCode
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Once that finishes, don’t neglect the two steps displayed in the terminal. You’ll need to run two commands which can be copied and pasted, and then run by hitting “return”
Everything that is displayed on the first line that looks like the code below (you’ll see your home directory after the “>>” make sure that you copy that too.)
(echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> <your home directory>
Everything on the second line
eval "$(/opt/homebrew/bin/brew shellenv)"
I do like to complete the “Hello World” Tutorial on a fresh build, but you by no means need to do so.
python3.11 -m pip install --upgrade pip
Getting Started with OpenAI
Register for an Open AI Account (if you don’t have one already)
Return to VSCode and Install “OpenAI”
Note: If you’ve set up your environment differently than the steps I outlined above, this input may be different for you
python3 -m pip install openai
Copy the example code from Open AI (make sure you use the Python script)
import os import openai openai.organization = "org-Rw9He2WlfsUmXrXLU27JLuPK" openai.api_key = os.getenv("OPENAI_API_KEY") openai.Model.list()
Two edits I made to this code:
- Comment out ‘openai.organization’ as I don’t have an Org ID currently
- If you do sign up for a Team Open AI account, you should update this “org_id”
- The method that this code is using above is attempting to store your API key in a variable. This is more secure, however since I am the only one using this file, I hardcoded the API key
- USE CAUTION WITH THIS METHOD IF SHARING THIS SCRIPT WITH OTHERS
The resulting code with edits looks like the following
import os import openai #openai.organization = "YOUR_ORG_ID" openai.api_key = 'YOUR_API_KEY' openai.Model.list()
Generate an API Key in your OpenAI account. Copy and paste that key in between the ‘ ’ in the above code
If you can run this code without an error, you have successfully authenticated and retrieved the Model List
One more change to the code:
- Comment out ‘openai.Model.list()’ as this is not required for image generation
Retrieve the image generation script from OpenAI
response = openai.Image.create( prompt="a white siamese cat", n=1, size="1024x1024" ) image_url = response['data']['url']
I made another edit for ease of displaying the link
Altogether, your code should look like the following (don’t forget to update your API key)
import os import openai #openai.organization = "YOUR_ORG_ID" openai.api_key = 'YOUR_API_KEY' #openai.Model.list() #response = openai.Image.create( prompt="a baby panda", n=1, size="1024x1024" ) image_url = response['data']['url'] print(response)
‘n’ refers to the number of images you would like to generate. This will accept a value from 1-10
‘size’ corresponds to the image dimensions you would like. You can choose between:
- 256 x 256
- 512 x 512
- 1024 x 1024
Now that you have your code compiled, all that’s left is to try it! Get started with the ‘prompt’ by adding your idea in between the “ “ on line 8
prompt = "your great idea here"
Things to Note
- The images that you generate are only accessible via the URL for one hour, so if you love something, make sure that you download it
- You have a limited number of calls that you are allotted on your OpenAI account. You can track how many you’ve made here.
- Another fun use of AI; you can have this code explained to you by pasting it into OpenAI’s chatbot or Denigma (I’ll give them a shout-out since they were my first tool for using AI for code description –
- Don’t forget that you can use Chat GPT to improve this code. One noticeable improvement would be to have the URL shortened and made easier to open in a web browser.
My favorite image so far
prompt = "a baby panda"