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) try: response = urllib.request.urlopen(request) img_name = "img.png" if (response.getcode() == 200): with open(img_name, "wb") as f: f.write(response.read()) # Writing content onto an image. return img_name except: return "failed" def print_hi(): openai.api_key = 'your API keys' response = openai.Image.create( prompt="A cute spotted dog", n=1, size="512x512" ) image_url = response['data']['url'] download_img(image_url) # Press the green button in the gutter to run the script. if __name__ == '__main__': print_hi() # See PyCharm help at https://www.jetbrains.com/help/pycharm/
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.