AI 生成圖片:The DALL-E API

目前 AI 生成圖片,產品成熟度依序為 Midjourney、Stable Diffusion、DALL·E 2。

我自己偏好 Stable Diffusion,主要是開放原始碼,主控權和未來彈性較佳,不用受制於人。

老師這節課講 DALL·E 2,下節會介紹 Stable Diffusion。

為什麼要介紹這個主題?

AI 生成圖像看起來似乎和本堂共學關聯不大,但如果你的產品/服務,需要 動態產生 相對應的圖片時,使用 DALL·E 2 是和 OpenAI API 搭配,最快速的解決方式。

老師之後會示範文字冒險遊戲:程式產生一個情境,玩家需要做出選擇,輸入文字,然後程式回應相對選回應及圖片,讓玩家繼續下一個選擇,依此類推。


DALL-E API 三個主要應用

  1. 創作:從文字 prompt 創作 AI 生成圖片

  2. 衍生:由現成圖片產生更多圖片

  3. 修改:由文字 prompt 修改現成圖片


創作

response = openai.Image.create(
  prompt="a white siamese cat",
  n=1,  # 1-10
  size="1024x1024"  # 256x256, 512x512, or 1024x1024
)
image_url = response['data'][0]['url']

線上直接玩玩看 DALL·E preview app

和其他類似服務一樣,你也可以看其他人的成品,以及其 prompt。


衍生

response = openai.Image.create_variation(
  image=open("corgi_and_cat_paw.png", "rb"),
  n=1,
  size="1024x1024"
)
image_url = response['data'][0]['url']

輸入圖片:小於 4MB 的 PNG 檔。


修改

一般稱為生成填圖,通常有兩個應用:

  1. Edit 增添物件(Inpainting):在圖片上指定一個區域,撰寫 prompt 加上新物件。

  2. Outpainting:繪圖區中圖片只佔一部分,撰寫 prompt 填補其他空白區域。



上圖來源:Image generation - OpenAI API

response = openai.Image.create_edit(
  image=open("sunlit_lounge.png", "rb"),
  mask=open("mask.png", "rb"),
  prompt="A sunlit indoor lounge area with a pool containing a flamingo",
  n=1,
  size="1024x1024"
)
image_url = response['data'][0]['url']

線上直接玩玩看 Editor in DALL·E preview app


存(圖)檔

兩種存圖檔的方式:

URL

# import 已省略
def save_image(url, image_name):
    image_directory_name = "images"
    image_directory = os.path.join(os.curdir, image_directory_name)

    if not os.path.isdir(image_directory):
        os.mkdir(image_directory)
        
    image_filepath = os.path.join(image_directory, image_name)
    
    image_content = requests.get(url).content
    
    with open(image_filepath, "wb") as image_file:
        image_file.write(image_content)


def get_image(prompt, image_name):
    res = openai.Image.create(
        prompt=prompt,
        size="512x512",
        n=1
    )
    image_url = res["data"][0]["url"]  ## =====> 取 URL 值
    save_image(image_url, image_name)

get_image(
    prompt="a 3d rendering of a melting peach popsicle against a rainbow background",
    image_name="popsicle2.png"
)

B64 JSON

def get_and_save_image(prompt, image_name):
    res = openai.Image.create(
        prompt=prompt,
        size="512x512",
        n=1,
        response_format="b64_json"
    )
    image_data = res["data"][0]["b64_json"]  ## =====> 取 B64 JSON 值
    
    image_directory_name = "images"
    image_directory = os.path.join(os.curdir, image_directory_name)

    if not os.path.isdir(image_directory):
        os.mkdir(image_directory)
        
    image_filepath = os.path.join(image_directory, image_name)
    
    decoded_img = base64.b64decode(image_data)
    
    with open(image_filepath, "wb") as file:
        file.write(decoded_img)
    
get_and_save_image("a painting of a vase full of lollipops in the style of van gogh", "vangogh.png")

課外補充:DALL-E Outpainting


課外補充:幾個 AI 生成圖片展示

版權因素(雖然目前國外法令規定,AI 生成圖片無法取得版權),僅簡單秀幾張大家參考。

如果大家對這個主題感興趣,可以加入臉書上的相關社團。和之前一樣,建議大家國內外的都加入,國內的方便討論、國外的觀賞作品。


課外補充:meme legends

Meme Legends Meetup Using Photoshop Generative Fill AI

Meme 傳奇全員大集合(使用 Photoshop Generative Fill AI 技術)

Meme Legends Meetup Using Photoshop Generative Fill AI 1

Meme Legends Meetup Using Photoshop Generative Fill AI 2

1個讚