Skip to content

Instantly share code, notes, and snippets.

@Alexander01998
Created December 8, 2023 15:10
Show Gist options
  • Save Alexander01998/68e36a26dc6748383e9ccff524404408 to your computer and use it in GitHub Desktop.
Save Alexander01998/68e36a26dc6748383e9ccff524404408 to your computer and use it in GitHub Desktop.
A basic python script for generating images with the DALL-E 3 API.
import os
import requests
import base64
import datetime
# --- Configuration variables ---
# A text description of the desired image. The maximum length is 4000 characters.
prompt = """
a fish riding a bicycle, photograph
""".strip()
# The size of the generated image. Must be one of "1024x1024" (default), "1024x1792", or "1792x1024".
image_size = "1792x1024"
# The quality of the image that will be generated. Must be one of "standard" (default) or "hd".
# HD creates images with finer details and greater consistency across the image.
quality = "hd"
# The style of the generated image. Must be one of "vivid" (default) or "natural".
# Vivid causes the model to lean towards generating hyper-real and dramatic images.
# Natural causes the model to produce more natural, less hyper-real looking images.
style = "natural"
# Your OpenAI API key. You can generate one at https://platform.openai.com/api-keys.
# It is recommended to store the API key in an environment variable instead of hardcoding it here.
api_key = os.getenv("OPENAI_API_KEY")
# --- End of configuration variables ---
# DALL-E 3 pricing based on the resolution and quality
pricing = {
"1024x1024": {
"standard": 0.040,
"hd": 0.080
},
"1024x1792": {
"standard": 0.080,
"hd": 0.120
},
"1792x1024": {
"standard": 0.080,
"hd": 0.120
},
}
# Print the configuration
print(f"Prompt: {prompt}")
print(f"Image size: {image_size}")
print(f"Quality: {quality}")
print(f"Style: {style}")
# Calculate the expected cost
expected_cost = pricing[image_size][quality]
print(f"Expected cost for generating this image: ${expected_cost:.3f}")
if input("Continue? (Y/n) ").lower() == "n":
exit()
# API endpoint and headers
api_endpoint = "https://api.openai.com/v1/images/generations"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
# API request body
data = {
"model": "dall-e-3",
"prompt": prompt,
"n": 1,
"size": image_size,
"quality": quality,
"style": style,
"response_format": "b64_json"
}
# Make the API request
response = requests.post(api_endpoint, headers=headers, json=data)
# Handle the response
if response.status_code == 200:
response_json = response.json()
image_data = response_json["data"][0]
if "revised_prompt" in image_data:
print(f"Revised prompt: {image_data['revised_prompt']}")
revised_suffix = "_revised"
else:
revised_suffix = ""
# Decode the base64-encoded image and save it as a PNG file
image_b64 = image_data["b64_json"]
image_decoded = base64.b64decode(image_b64)
# Get the current timestamp for the filename
timestamp = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
filename = f"{timestamp}_dalle3_{quality}_{style}{revised_suffix}.png"
with open(filename, "wb") as image_file:
image_file.write(image_decoded)
print(f"Image saved as {filename}")
else:
print(f"Failed to generate image: {response.status_code} - {response.text}")
print(f"URL: {api_endpoint}")
print(f"Request body: {data}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment