Can ChatGPT generate images? Image generation based on ChatGPT.

How did Chat GPT suddenly become so popular? When we open OpenAI’s website, we can see that this product is already the third generation of iterative calculation products, and it has been silently developed for three years.I deeply admire the research and dedication of experts in their field. However, we won’t be reviewing the history of Chat GPT today or delving into the underlying technology.We only look at one function that could completely disrupt the livelihoods of ordinary graphic designers and image design engineers, which is generating images automatically based on descriptions.

OpenAI has opened up its API for public use and also provides SDKs based on Python and Node.js, making it easy for users to access OpenAI’s interface to generate desired images.

First, you need to install the OpenAI library.

pip install openai

Then simply copy and paste the following code into a .py file with any filename.

# This is a sample Python script.
import openai
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import urllib.request

def download_img(img_url):
    request = urllib.request.Request(img_url)
        response = urllib.request.urlopen(request)
        img_name = "img.png"
        if (response.getcode() == 200):
            with open(img_name, "wb") as f:
                f.write(  # Writing content onto an image.
            return img_name
        return "failed"

def print_hi():
    openai.api_key = 'your API keys'
    response = openai.Image.create(
        prompt="A cute spotted dog",
    image_url = response['data'][0]['url']

# Press the green button in the gutter to run the script.
if __name__ == '__main__':

# See PyCharm help at

To apply for API keys, you can do so by clicking on the “Personal” button in the top right corner of the screen and then selecting “view API keys” as shown in the image below.

After obtaining the API keys, you can run to get the results.

Look, we have obtained a cute picture of a spotted dog, and if we are not satisfied with this picture, we can generate 10 pictures at once by modifying n = 10.In addition, you can also choose the size of the image. The currently supported sizes are 256×256, 512×512, or 1024×1024 pixels.

If we want more complex images, we can modify the prompt description to generate more complex images, such as “a small bridge over a flowing stream next to a house”, which can generate an image like the following:

The obtained image matches our idea.

What's your reaction?

Related Posts

Load More Posts Loading...No More Posts.