PythonでDALL·E 3のAPIを使って画像を生成することができます。
通常はopenaiを使いますが、それを使わずにAPIを直に扱って、画像を生成してみます。
正確には画像が生成されたURLを取得します。
import requests
import json
def main():
your_prompt = "可愛い子猫の写真"
image_url = get_image_url(your_prompt)
print(image_url)
def get_image_url(your_prompt):
api_key = "sk-xxxxx"
endpoint = "https://api.openai.com/v1/images/generations"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
data = {
"model": "dall-e-3",
"prompt": your_prompt
}
response = requests.post(
endpoint,
data = json.dumps(data),
headers = headers
)
result = response.json()
image_url = result["data"][0]["url"]
return image_url
if __name__ == "__main__":
main()
import json
def main():
your_prompt = "可愛い子猫の写真"
image_url = get_image_url(your_prompt)
print(image_url)
def get_image_url(your_prompt):
api_key = "sk-xxxxx"
endpoint = "https://api.openai.com/v1/images/generations"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
data = {
"model": "dall-e-3",
"prompt": your_prompt
}
response = requests.post(
endpoint,
data = json.dumps(data),
headers = headers
)
result = response.json()
image_url = result["data"][0]["url"]
return image_url
if __name__ == "__main__":
main()
URLは次のようになります。
https://oaidalleapiprodscus.blob.core.windows.net/private/org-iEeCj7B17c4YA0RUHLYhtnFK/user-rRPiAVp04EV7RM6KhUH7MchQ/img-HuOAtilCuVGIp9NCNoNF24XL.png?st=2024-03-06T13%3A40%3A44Z&se=2024-03-06T15%3A40%3A44Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2024-03-05T15%3A04%3A27Z&ske=2024-03-06T15%3A04%3A27Z&sks=b&skv=2021-08-06&sig=Tb2pA%2BrikZzztdXTc5ygw4nOjm%2BjP8kzyuBbzK5u4Yo%3D
コメント