Files
microsoft--generative-ai-fo…/09-building-image-applications/python/oai-app-variation.py
T
wehub-resource-sync 3fbbd7970c
Code Quality / Python Lint & Format (push) Has been cancelled
Code Quality / Python Tests (push) Has been cancelled
Code Quality / JavaScript/TypeScript Lint (advisory) (push) Has been cancelled
Security Scan / CodeQL Analysis (python) (push) Has been cancelled
Security Scan / Dependency Review (push) Has been cancelled
Security Scan / CodeQL Analysis (javascript-typescript) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:43:57 +08:00

39 lines
1005 B
Python

from openai import OpenAI, BadRequestError
import os
import requests
from PIL import Image
import dotenv
# import dotenv
dotenv.load_dotenv()
client = OpenAI()
image_dir = os.path.join(os.curdir, 'images')
# Initialize the image path (note the filetype should be png)
image_path = os.path.join(image_dir, 'generated-image.png')
# ---creating variation below---
try:
print("LOG creating variation")
response = client.images.create_variation(
image=open("generated-image.png", "rb"),
n=1,
size="1024x1024"
)
image_path = os.path.join(image_dir, 'generated_variation.png')
image_url = response.data[0].url
print("LOG downloading image")
generated_image = requests.get(image_url).content # download the image
with open(image_path, "wb") as image_file:
image_file.write(generated_image)
# Display the image in the default image viewer
image = Image.open(image_path)
image.show()
except BadRequestError as err:
print(err)