Web検索はbingがおすすめ!

あらかじめ設定した複数のプロンプトと設定を切り替えて自動で画像を生成する - Stable Diffusion

あらかじめ設定した複数のプロンプトと設定で自動で切り替えて画像を生成する手順を紹介します。

概要

モデルの検証などで、複数の同じプロンプトや出力設定で何回も画像を生成したい場合があります。
数が少ないうちは、毎回UIを設定すればよいですが、種類が増えてくると手動で設定するのは大変です。 この記事では、Stable Diffusion WebUIの Scripts の "Prompts from file or textbox" の機能を利用してあらかじめ設定したプロンプトや設定を読み込んで、 自動で画像を複数生成する手順を紹介します。

手順

設定ファイルの作成

はじめに画像を自動で生成するためのプロンプトや出力設定を記述したファイルを作成します。
次の書式を利用します。
--width (画像の出力幅) --height (画像の出力高さ) --sampler_name "(サンプラー名)" --n_iter (出力数) --outpath_samples (画像の保存先パス) --outpath_grids (グリッド画像の保存先パス) --do_not_save_samples false --do_not_save_grid false --prompt "(プロンプト)" --negative_prompt "(ネガティブプロンプト)"
上記の書式は一例です。記述しない場合は、WebUIの設定が利用されるため、最小限の場合は以下の書式でも動作します。
省略した場合、--do_not_save_samplesfalse--do_not_save_gridtrue に設定されるようです。
--prompt "(プロンプト)" --negative_prompt "(ネガティブプロンプト)"

実際に作成したファイルの例です。

画像を c:\data\images に、グリッド画像をc:\data\gridsに出力する設定です。
--width 1024 --height 1024 --sampler_name "Euler a" --n_iter 4 --outpath_samples c:\\data\\images --outpath_grids c:\\data\\grids --do_not_save_samples false --do_not_save_grid false --prompt "duck, forest, masterpiece" --negative_prompt "worst quality"
--width 1024 --height 1024 --sampler_name "Euler a" --n_iter 4 --outpath_samples c:\\data\\images --outpath_grids c:\\data\\grids --do_not_save_samples false --do_not_save_grid false --prompt "duck, desert, masterpiece" --negative_prompt "worst quality"
--width 1024 --height 1024 --sampler_name "Euler a" --n_iter 4 --outpath_samples c:\\data\\images --outpath_grids c:\\data\\grids --do_not_save_samples false --do_not_save_grid false --prompt "duck, ocean, masterpiece" --negative_prompt "worst quality"

画像はデフォルトのoutputsディレクトリに出力する場合の例です。
--width 1024 --height 1024 --sampler_name "Euler a" --n_iter 16 --do_not_save_samples false --do_not_save_grid false --prompt "duck, forest, masterpiece" --negative_prompt "worst quality"
--width 1024 --height 1024 --sampler_name "Euler a" --n_iter 16 --do_not_save_samples false --do_not_save_grid false --prompt "duck, desert, masterpiece" --negative_prompt "worst quality"
--width 1024 --height 1024 --sampler_name "Euler a" --n_iter 16 --do_not_save_samples false --do_not_save_grid false --prompt "duck, ocean, masterpiece" --negative_prompt "worst quality"

画像の生成

Stable Diffusion WebUIを表示します。txt2imgのタブをクリックして選択します。


ページを下にスクロールします。[Script]のエリアのドロップダウンリストボックスをクリックします。


ドロップダウンリストが表示されますので、リストの[Prompts from file or textbox]の項目をクリックして選択します。


選択するとドロップダウンボックスの下部に設定項目が表示されます。


先に作成したファイルを[ここにファイルをドロップ]のエリアにドロップするか、[Upload prompt inputs]のボタンをクリックして ファイルをアップロードします。


ファイルの内容が、[List of prompt inputs]のテキストボックスに表示されます。


使用したいモデルを選択した状態で、プロンプトのテキストボックスは空のまま、[Generate]ボタンをクリックします。 画像の生成が始まります。設定ファイルに記述したプロンプトのアヒルの画像が生成できています。


生成が完了した状態です。


(Stable Diffusionの配置ディレクトリ)\outputs\txt2img-grids\(生成日の日付 yyyy-mm-dd) のディレクトリを開きます。 生成されたグリッド画像が保存されています。


以下の設定で出力パスを設定した場合の結果を確認します。
--width 1024 --height 1024 --sampler_name "Euler a" --n_iter 16 --outpath_samples c:\\data\\images --outpath_grids c:\\data\\grids --do_not_save_samples false --do_not_save_grid false --prompt "duck, forest, masterpiece" --negative_prompt "worst quality"
--width 1024 --height 1024 --sampler_name "Euler a" --n_iter 16 --outpath_samples c:\\data\\images --outpath_grids c:\\data\\grids --do_not_save_samples false --do_not_save_grid false --prompt "duck, desert, masterpiece" --negative_prompt "worst quality"
--width 1024 --height 1024 --sampler_name "Euler a" --n_iter 16 --outpath_samples c:\\data\\images --outpath_grids c:\\data\\grids --do_not_save_samples false --do_not_save_grid false --prompt "duck, ocean, masterpiece" --negative_prompt "worst quality"

c:\dataディレクトリにimagesgrids のディレクトリが作成されます。


imagesディレクトリ内には生成日の日付のディレクトリが作成されます。


ディレクトリ内にバッチ処理で生成した画像が保存されます。


gridsディレクトリも日付のディレクトリが作成され、ディレクトリ内に生成結果の画像が保存されます。



あらかじめ設定したプロンプトと設定を切り替えて画像をバッチ生成できました。
著者
iPentecのメインデザイナー
Webページ、Webクリエイティブのデザインを担当。PhotoshopやIllustratorの作業もする。
掲載日: 2023-07-27
iPentec all rights reserverd.