edge lab LIVE! starts Tuesday in Munich. Grab a workshop seat and take home the board you build on. Code FRIEND-HACKSTER saves 20%. () Learn more!
Liège 360
Published © CC BY-NC-SA

Python3 OpenAi image creator

Python3 Script for Raspberry Pi to create images via OpenAI.

IntermediateShowcase (no instructions)-60 minutes163
Python3 OpenAi image creator

Things used in this project

Hardware components

Raspberry Pi 4 Model B
Raspberry Pi 4 Model B
×1

Software apps and online services

Raspbian
Raspberry Pi Raspbian

Story

Read more

Code

gen.py

Python
import sys
import openai
import webbrowser
import requests

openai.api_key="     "

def img_gen(query):
    response = openai.Image.create(
        prompt=query,
        n=1,
        size="1024x1024"
    )
    return response['data'][0]['url']

# main program
exit_conditions = ("exit")
get_conditions = ("get")
print_conditions = ("print")
open_conditions = ("open")
new_conditions = ("new")

while True:
     query= input("Enter the desired keywords separated by a comma to generate the image, or type 'exit' followed by ENTER to quit: ")
     if query in exit_conditions:
         sys.exit()

     url = img_gen(query)

     while True:
         query = input("Type 'open' followed by ENTER to view the image in browser, 'get' followed by ENTER to download the generated image, 'print' followed by ENTER to show the URL, 'new' followed by ENTER to generate a new image or 'exit' followed by ENTER to quit: ")
         if query in exit_conditions:
             sys.exit()
         if query in get_conditions:
             response = requests.get(url)
             with open('picopenai.png', 'wb') as f:
                 f.write(response.content)
             print("The image 'picopenai' is downloaded in png format on your computer !!!")
         if query in print_conditions:
             print(url)
         if query in open_conditions:
             webbrowser.open(url)
         if query in new_conditions:
             break
         else:
             print("    ")

Credits

Liège 360
23 projects • 0 followers

Comments