Created
July 6, 2023 14:03
-
-
Save richarddun/025ba0f04bea2d1dee47ed7bf925a28a to your computer and use it in GitHub Desktop.
Basic OpenAI image gen (Dall-e 2) from cli using python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This Python file uses the following encoding: utf-8 | |
import openai | |
import requests | |
import sys | |
from PIL import Image | |
# sample usage of Dalle-2 via Python | |
# from CLI with sys for args | |
variations = 4 | |
response = openai.Image.create( | |
prompt=f"{sys.argv[1].strip()}", | |
n=variations, | |
size="1024x1024" | |
) | |
image_urls = response['data'] | |
local_file_list = [] | |
for index,url in enumerate(image_urls,1): | |
filename = f"{sys.argv[1].replace(' ', '')[:10]}variation-{index}.png" | |
r = requests.get(url['url'], stream=True) | |
local_file_list.append(filename) | |
with open(filename, 'wb') as f: | |
f.write(r.content) | |
f.close() | |
# open image(s) and display it(them) | |
for image in local_file_list: | |
img = Image.open(image) | |
img.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment