콘텐츠로 이동

40. 이미지 생성(출력) 활용

Video

준비 중

Note

생성된 이미지 저장 예시

  • stream이 아닌 경우 이미지 생성 툴 옵션에서 Partial images를 None으로 설정해야 함.
    test.py
    import yaml
    from dotenv import load_dotenv
    from openai import OpenAI
    
    with open("config.yaml") as f:
        config = yaml.safe_load(f)
    
    load_dotenv()
    client = OpenAI()
    
    response = client.responses.create(
        input="고양이 이미지 생성해줘",
        **config
    )
    
    print("Assistant: ", response.output_text)
    
    import base64
    for output in response.output:
        if output.type == "image_generation_call":
            image_base64 = output.result
            image_format = output.output_format
            with open(f"test/output_image.{image_format}", "wb") as f:
                f.write(base64.b64decode(image_base64))
    
  • 이미지 생성 툴 옵션에서 Partial images를 1 이상으로 설정해야 부분 이미지를 받을 수 있음.
    test.py
    import yaml
    from dotenv import load_dotenv
    from openai import OpenAI
    
    with open("config.yaml") as f:
        config = yaml.safe_load(f)
    
    load_dotenv()
    client = OpenAI()
    
    response = client.responses.create(
        input="고양이 이미지 생성해줘",
        stream=True,
        **config
    )
    
    print("Assistant: ", end="", flush=True)
    
    import base64
    for event in response:
        if event.type == "response.output_text.delta":
            print(event.delta, end="", flush=True)
        elif event.type == "response.completed":
            previous_response_id = event.response.id
        elif event.type == "response.image_generation_call.partial_image":
            image_base64 = event.partial_image_b64
            image_format = event.output_format
            with open(f"test/output_image.{image_format}", "wb") as f:
                f.write(base64.b64decode(image_base64))
    

Resources