Skip to content

Instantly share code, notes, and snippets.

@richarddun
Created July 6, 2023 14:08
Show Gist options
  • Save richarddun/2ddb5f7f300af6282a829ee0381fe465 to your computer and use it in GitHub Desktop.
Save richarddun/2ddb5f7f300af6282a829ee0381fe465 to your computer and use it in GitHub Desktop.
Sample python script to take an image as input and send to dall-e 2 from cli
# This Python file uses the following encoding: utf-8
import openai
import requests
import sys
from PIL import Image
from uuid import uuid4
import os
image_to_vary = sys.argv[1]
try:
variations = sys.argv[2]
except IndexError:
variations = 4
# use uuid to keep variation filenames unique locally
runsum = f'{uuid4()}'[:8]
response = openai.Image.create_variation(
image=open(os.path.join(os.getcwd(), image_to_vary), "rb"),
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-{runsum}-{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