2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

画像加工に適したFLUX.1 KontextをAPIで呼び出してImage-to-Imageで画像を変換してみた

Posted at

Supershipの名畑です。ヒカルの碁 原画展は本当に良かった。一枚一枚の絵を凝視せずにいられないクオリティでした。

はじめに

FLUX.1 Kontextは従来の画像生成モデルとは異なり、コンテキスト内で画像を生成するアプローチを採用しているとのこと。これにより、テキストや画像から視覚的な概念を抽出および修正することができ、一貫性のある画像生成が可能となりました。また、主要な画像生成モデルと比べて最大8倍高速に画像を生成できるとされています。

高品質かつ高速な画像生成AI「FLUX.1 Kontext」が登場したので使ってみた、テキストと画像の入力に対応しアニメ風も実写風も生成可能 - GIGAZINE

Stable Diffusionのオリジナル開発陣によって立ち上げられたBlack Forest Labsが、新たな画像生成モデルFLUX.1 Kontextをリリースしました。
Text-to-ImageだけではなくImage-to-Imageにも対応しており、画像の一貫性が売りとのこと。

すでにリリースから2ヶ月経ってしまっていますが、備忘も兼ねてFLUX.1 Kontextで画像加工を試した結果を残しておきます。

公式情報

FLUX.1 Kontextについて詳しくは以下をご参照ください。

Today, we are excited to release FLUX.1 Kontext, a suite of generative flow matching models that allows you to generate and edit images. Unlike existing text-to-image models, the FLUX.1 Kontext family performs in-context image generation, allowing you to prompt with both text and images, and seamlessly extract and modify visual concepts to produce new, coherent renderings.

Black Forest Labs - Frontier AI Lab

maxprodevの3つがありますが、今回はproで試してみます。

FLUX.1 Kontext [pro] - A pioneer for fast, iterative image editing

速さが売りのモデルです。

コード

生成に用いたコードは過去記事「Stable Diffusionのオリジナル開発陣による画像生成AIモデル最新版FLUX 1.1 [pro]のWeb APIを呼んでいくつかの画像を生成してみた」のものを元とした以下となります。

import os
import requests
import time
import base64

with open("original_image.png", "rb") as image_file:  # 画像ファイルまでのパス
    base64_image = base64.b64encode(image_file.read()).decode('utf-8')

# リクエスト
request = requests.post(
    'https://api.bfl.ai/v1/flux-kontext-pro',
    headers={
        'accept': 'application/json',
        'x-key': os.environ.get("BFL_API_KEY"),
        'Content-Type': 'application/json',
    },
    json={
        'prompt': 'Change a woman\'s hair to blonde',
        "input_image": base64_image,
    },
).json()
print(request)
request_id = request["id"]

# 生成完了を待つ
while True:
    time.sleep(0.5)
    result = requests.get(
        request['polling_url'],
        headers={
            'accept': 'application/json',
            'x-key': os.environ.get("BFL_API_KEY"),
        },
        params={
            'id': request_id,
        },
    ).json()
    if result["status"] == "Ready":
        print(f"Result: {result['result']['sample']}")
        break
    else:
        print(f"Status: {result['status']}")

プロンプトに「Change a woman's hair to blonde」と指定しました。「女性の髪を金髪に変えて」ですね。

APIの詳細はImage Editing - Black Forest Labsをご参照ください。

元画像

過去に生成した以下の画像を元にして変換をかけてみます。

original_image.png

結果

結果は以下です。

result_image.png

かなり意図通りの画像が生成されました。

素晴らしい。

最後に

AIの進化が早すぎてかなり感覚が麻痺していますが、本当に高速で便利になっていると実感します。

宣伝

SupershipのQiita Organizationを合わせてご覧いただけますと嬉しいです。他のメンバーの記事も多数あります。

Supershipではプロダクト開発やサービス開発に関わる方を絶賛募集しております。
興味がある方はSupership株式会社 採用サイトよりご確認ください。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?