書籍5:画像生成アプリを作ろう(作成中止)

DALL-Eは同じキーで使えるが、認証がさらに厳しく身分証の写真の提出を要求されるため今回は作成をあきらめて、画像生成のアプリは作らないこととした。

画像生成AIのDALL-Eを利用して、文章を指定し画像を生成するアプリtext_to_image.pyを実行したら動かないようです。原因は?また、DALL-Eは先に取得したChatGTPのAPIキーで動くのでしょうか?

importbase64
fromioimportBytesIO
fromPILimportImage
fromopenaiimportOpenAI
# 画像生成を行う関数 --- (*1)
deftext_to_image(savefile, prompt, model="dall-e-3",
size="1024x1024", quality="standard", style="vivid"):
# APIを呼び出す --- (*2)
client=OpenAI()
response=client.images.generate(
prompt=prompt, # プロンプト
model=model, # モデルを指定(dall-e-2/dall-e-3)
size=size, # 画像サイズ(1024x1024/1024×1792/1792×1024)
quality=quality, # 画質(standard/hd)
style=style, # スタイル(natural/vivid)
response_format="b64_json", # 応答形式(url/b64_json)
n=1# 生成する画像の数
    )
# Base64で得た画像をファイルに保存 --- (*3)
image_data=base64.b64decode(response.data[0].b64_json)
image=Image.open(BytesIO(image_data))
image.save(savefile)
# 実際に使われたプロンプトを表示 --- (*4)
print(response.data[0].revised_prompt)
if__name__=="__main__":
# プロンプトを指定して画像を生成 --- (*5)
text_to_image(
"text_to_image_test.png",
"学校の教室にいるウサギとヒロインをアニメ風に描いてください。",
quality="hd")
PS C:\Users\DELL> cd c:/Users/DELL/pydesktop/src/ch5
PS C:\Users\DELL\pydesktop\src\ch5> & C:/Users/DELL/AppData/Local/Programs/Python/Python313/python.exe c:/Users/DELL/pydesktop/src/ch5/text_to_image.py
Please draw an image in an Anime style, featuring a rabbit and a hero character who is a female student, in a school classroom setting. The rabbit should be cutely depicted, perhaps sitting on a desk or hopping around, and the female student hero should have an energetic expression, possibly wearing the typical uniform of a Japanese high school.
PS C:\Users\DELL\pydesktop\src\ch5>

コメント