How to do batch img2img job of many img/prompt pairs -- automatic1111 stable diffusion

6.3k Views Asked by At

there

With automatic1111 stable diffuison, I need to re-draw 100 images. Thanks to clip-interrogator, I've generated prompt text for each one of them. Next, I should to run img2img. However, I noticed that you cannot sigh the prompt for each image specifically with img2img batch tab.

Any suggestions about the task? What's the best and easy way to do it?

I tried to write a custom script about it as below:

import modules.scripts as scripts
import gradio as gr
import os
import random
from PIL import Image, ImageOps

from modules import images
from modules.processing import process_images, Processed
from modules.shared import opts, cmd_opts, state


def load_prompt_file(file):
    if file is None:
        lines = []
    else:
        lines = [x.strip() for x in file.decode('utf8', errors='ignore').split("\n")]
    return None, "\n".join(lines), gr.update(lines=3)

def list_files(dirname):
    filenames = [os.path.join(dirname, x) for x in sorted(os.listdir(dirname)) if not x.startswith(".")]
    return [file for file in filenames if os.path.isfile(file)]

class Script(scripts.Script):

    def title(self):
        return "1_img/1_prompt img2img"

    def show(self, is_img2img):
        return is_img2img

    def ui(self, is_img2img):
        different_seeds = gr.Checkbox(label='Use different seed for each picture', value=True, elem_id=self.elem_id("different_seeds"))

        prompt_txt = gr.Textbox(label="List of prompt inputs", lines=1, elem_id=self.elem_id("prompt_txt"))
        prompt_file = gr.File(label="Upload prompt inputs", type='binary', elem_id=self.elem_id("prompt_file"))
        prompt_file.change(fn=load_prompt_file, inputs=[prompt_file], outputs=[prompt_file, prompt_txt, prompt_txt])
        prompt_txt.change(lambda tb: gr.update(lines=3) if ("\n" in tb) else gr.update(lines=2), inputs=[prompt_txt], outputs=[prompt_txt])

        img_input_dir = gr.Textbox(label="Input directory", elem_id=self.elem_id("img2img_batch_input_dir"))
        img_output_dir = gr.Textbox(label="Output directory", elem_id=self.elem_id("img2img_batch_output_dir"))
        return [different_seeds, prompt_txt, img_input_dir, img_output_dir]

    def run(self, p, different_seeds, prompt_txt, img_input_dir, img_output_dir):
        prompt_lines = [x.strip() for x in prompt_txt.splitlines()]
        prompt_lines = [x for x in prompt_lines if len(x) > 0]
        print(prompt_lines)
        print(img_input_dir)
        print(img_output_dir)
        images_list = []
        seeds_list = []
        all_prompts = []
        all_negative_prompts = []
        save_normally = img_output_dir == ''
        p.do_not_save_grid = True
        p.do_not_save_samples = not save_normally

        run_per_image = p.n_iter * p.batch_size
        image_files = list_files(img_input_dir)
        image_files_num = len(image_files)
        print(f"Will process {image_files_num} images, creating {run_per_image} new images for each.")

        assert len(prompt_lines)==image_files_num

        state.job_count = image_files_num * run_per_image
        for n in range(image_files_num):
            i_image_file = image_files[n]
            i_img = Image.open(i_image_file)
            i_img = ImageOps.exif_transpose(i_img)
            i_prompt = prompt_lines[n]
            i_neg_prompt = ""
            for m in range(run_per_image):
                state.job = f"Iteration {n + 1}image, {n + 1}/{run_per_image} batch"
                images_list.append(i_img)
                seeds_list.append(int(random.randrange(4294967294)))
                all_prompts.append(i_prompt)
                all_negative_prompts.append(i_neg_prompt)

        processed = Processed(p, images_list, seeds_list, "", all_prompts=all_prompts, all_negative_prompts=all_negative_prompts)
        return processed

But it shows error as:

Error completing request
Arguments: ('task(uhmsex0ftvz0mjn)', 0, '', '', [], None, None, None, None, None, None, None, 20, 
0, 4, 0, 1, False, False, 1, 1, 7, 0.75, -1.0, -1.0, 0, 0, 0, False, 512, 512, 0, 0, 32, 0, '', '', 1, True, False, True, 'a couple of kids sitting at a table with shoes, a hyperrealistic painting, cg society contest winner, hyperrealism, ferrari f 4 0, trending on youtube, surprised expression, official product photo, based on a puma, color restoration, adorable!!!, 5 years old\na couple 
of young boys sitting next to each other, computer graphics, trending on pinterest, beakers full of liquid, spitting cushions from his mouth, blond hair green eyes, orange color, transparent water, plexus, puppets, three head one body, ‘luca’, tutorial, drinks, 12, kids, face shown, supersampled, mental alchemy\n', 'D:\\ad0116\\style-real', 'D:\\ad0116\\', '<ul>\n<li><code>CFG Scale</code> should be 2 or lower.</li>\n</ul>\n', True, True, '', '', True, 50, True, 1, 0, False, 4, 1, '<p 
style="margin-bottom:0.75em">Recommended settings: Sampling Steps: 80-100, Sampler: Euler a, Denoising strength: 0.8</p>', 128, 8, ['left', 'right', 'up', 'down'], 1, 0.05, 128, 4, 0, ['left', 'right', 'up', 'down'], False, False, False, False, '', '<p style="margin-bottom:0.75em">Will upscale the image by the selected scale factor; use width and height sliders to set tile size</p>', 64, 0, 2, 1, '', 0, '', True, False, False, 'Not set', True, True, '', '', '', '', '', 1.3, 'Not set', 
'Not set', 1.3, 'Not set', 1.3, 'Not set', 1.3, 1.3, 'Not set', 1.3, 'Not set', 1.3, 'Not set', 1.3, 'Not set', 1.3, 'Not set', 1.3, 'Not set', False, 'None', '<p style="font-weight:bold;margin-bottom:0.75em">Deforum v0.5-webui-beta</p>', '<p>This script is deprecated. Please use the full Deforum extension instead.<br>\nUpdate instructions:</p>', '<p>github.com/deforum-art/deforum-for-automatic1111-webui/blob/automatic1111-webui/README.md</p>', '<p>discord.gg/deforum</p>', False, 0, True, 384, 384, False, 2, True, True, False, False) {}
Traceback (most recent call last):
  File "E:\_Ai\stable-diffusion-webui\modules\call_queue.py", line 56, in f
    res = list(func(*args, **kwargs))
  File "E:\_Ai\stable-diffusion-webui\modules\call_queue.py", line 37, in f
    res = func(*args, **kwargs)
  File "E:\_Ai\stable-diffusion-webui\modules\img2img.py", line 66, in img2img
    image = init_img.convert("RGB")
AttributeError: 'NoneType' object has no attribute 'convert'

btw, I'm using python on windows. So I used "D:\ad0116\style-real" as the image input folder style.

ANY suggestions about how to fix or deal with the problem?

Thanks for your attention and help~

Best,

Zack

1

There are 1 best solutions below

1
On

You need an inital image, as the first run through has no img to img2img, hence the can't convert None.