Image Generation 이미지 생성

DALL-E3 : https://openai.com/index/dall-e-3/

using System;
using System.Collections;
using System.Collections.Generic;
using OpenAI_API;
using OpenAI_API.Chat;
using OpenAI_API.Models;
using OpenAI_API.Images;
using UnityEngine;
using UnityEngine.Networking;
using System.Threading.Tasks;

public class OpenAIImageMaker : MonoBehaviour
{
    private OpenAIAPI api;
    // Start is called before the first frame update
    private string prompt = "A drawing of a thinking bulb";
    void Start()
    {
        api = new OpenAIAPI(Environment.GetEnvironmentVariable("OPENAI_API_KEY", EnvironmentVariableTarget.User));
        //make_image();
    }
    private string image_url = "";

    public async void make_image()
    {
        image_url = "";
        image_texture = null;
        //var result = await api.ImageGenerations.CreateImageAsync(new ImageGenerationRequest(prompt, OpenAI_API.Models.Model.DALLE3, ImageSize._1024, "hd"));
        //DALL-E3
        var result = await api.ImageGenerations.CreateImageAsync(new ImageGenerationRequest(prompt, 1, ImageSize._512));
        //DALL-E1
        //hd, standard

        Debug.Log(result.Data[0].Url);
        image_url = result.Data[0].Url;
        StartCoroutine(DownloadImage(image_url));
    }

    IEnumerator DownloadImage(string MediaUrl)
    {   
        Debug.Log("DownloadImage start");
        UnityWebRequest request = UnityWebRequestTexture.GetTexture(MediaUrl);
        yield return request.SendWebRequest();
        if(request.isNetworkError || request.isHttpError) 
            Debug.Log(request.error);
        else
            image_texture = ((DownloadHandlerTexture) request.downloadHandler).texture;
        
        Debug.Log("DownloadImage end");
    } 

    private int font_size = 20;
    Texture image_texture = null;

    private void OnGUI(){

        GUIStyle LabelStyle = new GUIStyle(GUI.skin.label);
	    LabelStyle.fontSize = font_size;
        GUIStyle TextAreaStyle = new GUIStyle(GUI.skin.textArea);
        TextAreaStyle.fontSize = font_size;
        GUIStyle buttonStyle = new GUIStyle(GUI.skin.button);
        buttonStyle.fontSize = font_size;

        // draw label right of screen


        if(image_texture != null){
            GUI.DrawTexture(new Rect(Screen.width-400, 80, 400, 400), image_texture);
            //GUI.TextField(new Rect(Screen.width-200, 240, 200, 400), image_url, LabelStyle);
            if(GUI.Button(new Rect(Screen.width-400, 480, 400, 40), "copy url", buttonStyle)){
                TextEditor te = new TextEditor();
                te.text = image_url;
                te.SelectAll();
                te.Copy();
            }
        }
        prompt = GUI.TextField(new Rect(Screen.width-400, 0, 400, 40), prompt, TextAreaStyle);
        if(GUI.Button(new Rect(Screen.width-400, 40, 400, 40), "make image", buttonStyle)){
            make_image();
        }
        
    }
}
  • For DALL-E 3, only 1024x1024, 1024x1792, or 1792x1024 is allowed.