0
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?

More than 1 year has passed since last update.

webuiapiを使用した画像のバッチ生成ツール

Posted at

概要

StableDiffusion Web UIを使用していて、複数のベースモデルを切り替えながら沢山の画像を生成しようと思うとちょこちょこ手作業が発生するので、登録しておいた条件でバッチ処理できるツールを作りました。

・SDexecWebUiApi_multiModel.py ... 1つのプロンプトに対して登録してあるベースモデル分画像を生成します
・SDexecWebUiApi_multiPrompt.py ... 1つのベースモデルに対して、登録してあるプロンプト分画像を生成します
※どちらも満たせる汎用ツールを作るとデータの管理が面倒そうだったのでとりあえず用途別にシンプルに

参考情報

・webuiapiについてはこちらの記事で知りました。
https://www.kyoukasho.net/entry/webuiapi

・webuiapi本家
https://github.com/mix1009/sdwebuiapi

前提条件

・ローカル環境にStable Diffusion Web UIがインストールされていること
 ※どこぞのクラウド上に対してもいけるかもしれませんが試してません→webuiapi本家を参照
・上記記事(1)~(3)にも書いてありますが、webuiapiのインストールと起動オプションの追加が必要です
・エラー処理を全く入れていないので動く条件を設定してください。
・置き場所は選ぶ?

バッチ生成ツールの実行

python SDexecWebUiApi_multiModel.py

※どちらのツールも特に引数はないです

変更のポイント

・それぞれのファイルのTODOを見てください
・このツールで使用していないオプションも上書き可能です→webuiapiのtxt2img()メソッドの定義参照

サンプルコード

・SDexecWebUiApi_multiModel.py

# Stable diffusion Web UI をPythonから実行
# 注意点:エラー処理をまったく入れていないのでGUIで出力可能な設定を調べてから設定してください
import webuiapi
import datetime
import os

# -------------------------------------------------------------------------------------------------------------
# 
# - 生成条件 -
# TODO:環境に合わせて変更
myPrompt="(masterpiece, best quality:1.2),girl,anime"
mySeed = -1
myNegativePrompt ="worst quality, low quality"
mySampler ="DPM++ 2M karras"
myHeight = 768
# インストール済みのモデルのうちどれを使用するか定義する
executeModels=[
    'AOM3.safetensors [d124fc18f0]', 
    'anything-v4.0-pruned.safetensors [69528490df]', 
    'bra_v5.safetensors [ac68270450]', 
    'majicmixRealistic_v4.safetensors [d819c8be6b]', 
    'xeroxrealmix_v35.safetensors [d7700eac56]'
    ]
# 出力先フォルダを指定
outputPath="./outputs/txt2img-images/"
# 1つのモデルに対して出力するファイル数
#
batchCount = 100
#
# -------------------------------------------------------------------------------------------------------------

#ライブラリのインスタンス生成
api = webuiapi.WebUIApi()

#現在のモデル取得→後で元に戻す
restoreModel = api.util_get_current_model()
print(restoreModel)

# TODO:入っているモデルを調べたいときにプリントしてみる
#myModels = api.util_get_model_names()
# print( executeModels )

# 全体スタート
#フォルダ作成:年月日_時分秒で作成
startTime = datetime.datetime.now()
imageFolder = outputPath + startTime.strftime("%Y%m%d_%H%M%S")
print( "Create Folder:" + imageFolder )
os.mkdir( imageFolder )

# モデル分回す
for currentModel in executeModels:
    # set model (use exact name)
    api.util_set_model( currentModel )
    # バッチ数ぶん回す
    seqNo = 0
    for outputCount in range( batchCount ):
        resultApi = api.txt2img( prompt= myPrompt,seed = mySeed, negative_prompt = myNegativePrompt, sampler_index = mySampler,  height=myHeight )
        # TODO:ファイル名はモデル名+日時+連番→自由に変更どうぞ
        curTime = datetime.datetime.now()
        imageName = imageFolder + "/" + currentModel + curTime.strftime("%Y%m%d_%H%M%S") + "-" + str(seqNo) + ".png"
        seqNo = seqNo + 1
        print("Create File:" + imageName)
        resultApi.image.save( imageName.replace(' ','') )

# 選択中のモデルに戻す
api.util_set_model( restoreModel )

・SDexecWebUiApi_multiPrompt.py

# Stable diffusion Web UI をPythonから実行
# 注意点:エラー処理をまったく入れていないのでGUIで出力可能な設定を調べてから設定してください
import webuiapi
import datetime
import os

# -------------------------------------------------------------------------------------------------------------
# 
# - 生成条件 -
# TODO:環境に合わせて変更
myPrompts=[
    # 1個目のプロンプト 
    "(masterpiece, best quality:1.2),little girl",
    # 最後のプロンプト
    "(masterpiece, best quality:1.2),girl,anime"
    ]
mySeed = -1
myNegativePrompt ="worst quality, low quality"
mySampler ="DPM++ 2M karras"
myHeight = 768
# 出力先のベースフォルダ指定
outputPath="./outputs/txt2img-images/"
# 1つのプロンプトに対して出力するファイル数
batchCount = 5
# -------------------------------------------------------------------------------------------------------------

#ライブラリのインスタンス生成
api = webuiapi.WebUIApi()

# 現在のモデルを取得→ファイル名に使用
currentModel = api.util_get_current_model()

# 全体スタート
#フォルダ作成:年月日_時分秒で作成
startTime = datetime.datetime.now()
imageFolder = outputPath + startTime.strftime("%Y%m%d_%H%M%S")
print( "Create Folder:" + imageFolder )
os.mkdir( imageFolder )

# プロンプト分回す
for myPrompt in myPrompts:
    # バッチ数ぶん回す
    seqNo = 0
    for outputCount in range( batchCount ):
        resultApi = api.txt2img( prompt= myPrompt,seed = mySeed, negative_prompt = myNegativePrompt, sampler_index = mySampler,  height=myHeight )
        # ファイル名はモデル名+日時+連番→自由に
        curTime = datetime.datetime.now()
        imageName = imageFolder + "/" + currentModel + curTime.strftime("%Y%m%d_%H%M%S") + "-" + str(seqNo) + ".png"
        seqNo = seqNo + 1
        print("Create File:" + imageName)
        resultApi.image.save( imageName.replace(' ','') )





0
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
0
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?