openai api 이미지 생성, 저장

yshaarj·2023년 6월 27일
0

어케 했음?

목록 보기
6/8

openai api를 사용하여 이미지를 생성하고 저장하는 방법
(참고 https://platform.openai.com/docs/guides/images)

import openai
from base64 import b64decode
from PIL import Image
from io import BytesIO

def img_create():

        prompt = "two cat and one dog in house"
      
        openai.api_key = openai api 키
        
        response = openai.Image.create(
            prompt=prompt, n=1, size="256x256", response_format="b64_json"
        )

        img_b64 = b64decode(response["data"][0]["b64_json"])
       
        Image.open(BytesIO(img_b64)).save("저장할 파일 이름")

img_create()

결과는 다음과 같다

0개의 댓글