Sure…import os
import base64
from openai import OpenAI
api_key = os.getenv(“OPENAI_API_KEY”)
if not api_key:
raise ValueError(“Please set the OPENAI_API_KEY environment variable.”)
client = OpenAI(api_key=api_key)
prompt = “”" My prompt is so detailed. It request about image has to be sleek, simple relevant illustrations, text overlay as given etc.. (Sorry that I can’t share the exact prompt)
“”"
try:
# Generate the image
result = client.images.generate(
model=“gpt-image-1”,
prompt=prompt,
size=“1024x1024”
)
image_base64 = result.data[0].b64_json
image_bytes = base64.b64decode(image_base64)
with open("output.png", "wb") as f:
f.write(image_bytes)
print("Image successfully saved as 'output.png'.")
except Exception as e:
print(f"An error occurred: {e}")