Skip to content

Instantly share code, notes, and snippets.

@jeffehobbs
Created November 22, 2023 23:25
Show Gist options
  • Save jeffehobbs/f9ee51909f4c58a501929aca25b3a07f to your computer and use it in GitHub Desktop.
Save jeffehobbs/f9ee51909f4c58a501929aca25b3a07f to your computer and use it in GitHub Desktop.
bulk generator of terrible AI images
# dallewood | jeffehobbs@gmail.com
import typer, configparser, os, shutil, requests, hashlib, openai
from tqdm import tqdm
app = typer.Typer()
OUTPUT_DIR = '/output'
OUTPUT_PROMPT_PREFIX = ''
# generate image from post text
def get_openai_image(prompt, num, start):
for i in tqdm(range(start, num)):
SCRIPT_PATH = os.path.dirname(os.path.abspath(__file__))
config = configparser.ConfigParser()
config.read(SCRIPT_PATH +'/apikeys.txt')
openai.api_key = config.get('apikeys', 'openai_apikey')
try:
response = openai.Image.create(prompt=f'{OUTPUT_PROMPT_PREFIX}{prompt}', model='dall-e-3', style='vivid', quality='hd', size="1792x1024")
except:
get_openai_image(prompt, num, start)
start = start + 1
image_url = response['data'][0]['url']
response = requests.get(image_url, stream=True)
file_hash = hashlib.md5(str(prompt).encode('utf-8')).hexdigest()
file_path = SCRIPT_PATH + '/output/' + file_hash + '_' + str(i) + '.png'
#print(f"file path : {file_path}")
with open(file_path, 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
del response
file_hash = hashlib.md5(str(prompt).encode('utf-8')).hexdigest()
file_path = SCRIPT_PATH + '/output/' + file_hash + '_0.png'
return file_path
@app.command()
def prompt(prompt: str, num: int):
print(f"\n---\nUsing the prompt '{OUTPUT_PROMPT_PREFIX}{prompt}', generate {num} images:\n---\n")
file_path = get_openai_image(prompt, num, 0)
print("...files begin at this path:")
print(file_path)
if __name__ == "__main__":
app()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment