2022-09-22 03:11:48 -06:00
|
|
|
import os
|
2023-07-10 11:08:23 -06:00
|
|
|
from contextlib import closing
|
2023-05-29 10:39:24 -06:00
|
|
|
from pathlib import Path
|
2022-09-22 03:11:48 -06:00
|
|
|
|
2022-09-07 20:35:26 -06:00
|
|
|
import numpy as np
|
2023-08-04 03:35:47 -06:00
|
|
|
from PIL import Image, ImageOps, ImageFilter, ImageEnhance, UnidentifiedImageError
|
2023-06-14 13:03:44 -06:00
|
|
|
import gradio as gr
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2023-08-08 12:28:34 -06:00
|
|
|
from modules import images as imgutil
|
2023-06-20 05:33:36 -06:00
|
|
|
from modules.generation_parameters_copypaste import create_override_settings_dict, parse_generation_parameters
|
2022-09-03 03:08:45 -06:00
|
|
|
from modules.processing import Processed, StableDiffusionProcessingImg2Img, process_images
|
|
|
|
from modules.shared import opts, state
|
2023-08-24 19:12:19 -06:00
|
|
|
from modules.sd_models import get_closet_checkpoint_match
|
2022-09-03 03:08:45 -06:00
|
|
|
import modules.shared as shared
|
|
|
|
import modules.processing as processing
|
|
|
|
from modules.ui import plaintext_to_html
|
2022-09-03 08:21:15 -06:00
|
|
|
import modules.scripts
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2022-09-22 03:11:48 -06:00
|
|
|
|
2023-07-08 06:10:10 -06:00
|
|
|
def process_batch(p, input_dir, output_dir, inpaint_mask_dir, args, to_scale=False, scale_by=1.0, use_png_info=False, png_info_props=None, png_info_dir=None):
|
2023-07-29 23:47:24 -06:00
|
|
|
output_dir = output_dir.strip()
|
2022-09-22 03:11:48 -06:00
|
|
|
processing.fix_seed(p)
|
|
|
|
|
2023-07-27 19:15:50 -06:00
|
|
|
images = list(shared.walk_files(input_dir, allowed_extensions=(".png", ".jpg", ".jpeg", ".webp", ".tif", ".tiff")))
|
2022-09-22 03:11:48 -06:00
|
|
|
|
2023-01-28 06:42:24 -07:00
|
|
|
is_inpaint_batch = False
|
|
|
|
if inpaint_mask_dir:
|
|
|
|
inpaint_masks = shared.listfiles(inpaint_mask_dir)
|
2023-06-02 05:58:10 -06:00
|
|
|
is_inpaint_batch = bool(inpaint_masks)
|
2023-06-05 02:08:57 -06:00
|
|
|
|
|
|
|
if is_inpaint_batch:
|
|
|
|
print(f"\nInpaint batch is enabled. {len(inpaint_masks)} masks found.")
|
2023-01-27 18:32:31 -07:00
|
|
|
|
2022-09-22 03:11:48 -06:00
|
|
|
print(f"Will process {len(images)} images, creating {p.n_iter * p.batch_size} new images for each.")
|
|
|
|
|
|
|
|
state.job_count = len(images) * p.n_iter
|
|
|
|
|
2023-06-20 05:50:02 -06:00
|
|
|
# extract "default" params to use in case getting png info fails
|
2023-06-20 05:33:36 -06:00
|
|
|
prompt = p.prompt
|
|
|
|
negative_prompt = p.negative_prompt
|
2023-06-20 05:50:02 -06:00
|
|
|
seed = p.seed
|
|
|
|
cfg_scale = p.cfg_scale
|
|
|
|
sampler_name = p.sampler_name
|
|
|
|
steps = p.steps
|
2023-08-24 19:12:19 -06:00
|
|
|
override_settings = p.override_settings
|
|
|
|
sd_model_checkpoint_override = get_closet_checkpoint_match(override_settings.get("sd_model_checkpoint", None))
|
2022-09-22 03:11:48 -06:00
|
|
|
for i, image in enumerate(images):
|
|
|
|
state.job = f"{i+1} out of {len(images)}"
|
2022-10-04 21:56:30 -06:00
|
|
|
if state.skipped:
|
|
|
|
state.skipped = False
|
2022-09-22 03:11:48 -06:00
|
|
|
|
|
|
|
if state.interrupted:
|
|
|
|
break
|
|
|
|
|
2023-03-31 02:54:42 -06:00
|
|
|
try:
|
|
|
|
img = Image.open(image)
|
2023-05-02 23:28:59 -06:00
|
|
|
except UnidentifiedImageError as e:
|
|
|
|
print(e)
|
2023-03-31 02:54:42 -06:00
|
|
|
continue
|
2022-10-25 02:39:59 -06:00
|
|
|
# Use the EXIF orientation of photos taken by smartphones.
|
2022-11-08 20:06:29 -07:00
|
|
|
img = ImageOps.exif_transpose(img)
|
2023-05-29 11:38:49 -06:00
|
|
|
|
2023-05-29 10:47:20 -06:00
|
|
|
if to_scale:
|
|
|
|
p.width = int(img.width * scale_by)
|
|
|
|
p.height = int(img.height * scale_by)
|
2023-05-29 11:38:49 -06:00
|
|
|
|
2022-09-22 03:11:48 -06:00
|
|
|
p.init_images = [img] * p.batch_size
|
|
|
|
|
2023-06-01 01:29:56 -06:00
|
|
|
image_path = Path(image)
|
2023-01-27 18:32:31 -07:00
|
|
|
if is_inpaint_batch:
|
|
|
|
# try to find corresponding mask for an image using simple filename matching
|
2023-06-01 01:29:56 -06:00
|
|
|
if len(inpaint_masks) == 1:
|
2023-01-27 18:32:31 -07:00
|
|
|
mask_image_path = inpaint_masks[0]
|
2023-06-01 01:29:56 -06:00
|
|
|
else:
|
|
|
|
# try to find corresponding mask for an image using simple filename matching
|
2023-06-01 05:44:55 -06:00
|
|
|
mask_image_dir = Path(inpaint_mask_dir)
|
2023-06-01 01:29:56 -06:00
|
|
|
masks_found = list(mask_image_dir.glob(f"{image_path.stem}.*"))
|
|
|
|
|
|
|
|
if len(masks_found) == 0:
|
2023-06-01 05:44:55 -06:00
|
|
|
print(f"Warning: mask is not found for {image_path} in {mask_image_dir}. Skipping it.")
|
|
|
|
continue
|
2023-06-01 01:29:56 -06:00
|
|
|
|
|
|
|
# it should contain only 1 matching mask
|
|
|
|
# otherwise user has many masks with the same name but different extensions
|
|
|
|
mask_image_path = masks_found[0]
|
|
|
|
|
2023-01-27 18:32:31 -07:00
|
|
|
mask_image = Image.open(mask_image_path)
|
|
|
|
p.image_mask = mask_image
|
|
|
|
|
2023-06-20 05:33:36 -06:00
|
|
|
if use_png_info:
|
|
|
|
try:
|
|
|
|
info_img = img
|
|
|
|
if png_info_dir:
|
|
|
|
info_img_path = os.path.join(png_info_dir, os.path.basename(image))
|
|
|
|
info_img = Image.open(info_img_path)
|
|
|
|
geninfo, _ = imgutil.read_info_from_image(info_img)
|
|
|
|
parsed_parameters = parse_generation_parameters(geninfo)
|
2023-07-08 06:42:00 -06:00
|
|
|
parsed_parameters = {k: v for k, v in parsed_parameters.items() if k in (png_info_props or {})}
|
|
|
|
except Exception:
|
|
|
|
parsed_parameters = {}
|
|
|
|
|
|
|
|
p.prompt = prompt + (" " + parsed_parameters["Prompt"] if "Prompt" in parsed_parameters else "")
|
|
|
|
p.negative_prompt = negative_prompt + (" " + parsed_parameters["Negative prompt"] if "Negative prompt" in parsed_parameters else "")
|
|
|
|
p.seed = int(parsed_parameters.get("Seed", seed))
|
|
|
|
p.cfg_scale = float(parsed_parameters.get("CFG scale", cfg_scale))
|
|
|
|
p.sampler_name = parsed_parameters.get("Sampler", sampler_name)
|
|
|
|
p.steps = int(parsed_parameters.get("Steps", steps))
|
2023-01-27 18:32:31 -07:00
|
|
|
|
2023-08-24 19:12:19 -06:00
|
|
|
model_info = get_closet_checkpoint_match(parsed_parameters.get("Model hash", None))
|
|
|
|
if model_info is not None:
|
|
|
|
p.override_settings['sd_model_checkpoint'] = model_info.name
|
|
|
|
elif sd_model_checkpoint_override:
|
|
|
|
p.override_settings['sd_model_checkpoint'] = sd_model_checkpoint_override
|
|
|
|
else:
|
|
|
|
p.override_settings.pop("sd_model_checkpoint", None)
|
|
|
|
|
2022-09-22 03:11:48 -06:00
|
|
|
proc = modules.scripts.scripts_img2img.run(p, *args)
|
|
|
|
if proc is None:
|
2023-07-29 09:34:04 -06:00
|
|
|
if output_dir:
|
|
|
|
p.outpath_samples = output_dir
|
|
|
|
p.override_settings['save_to_dirs'] = False
|
|
|
|
if p.n_iter > 1 or p.batch_size > 1:
|
|
|
|
p.override_settings['samples_filename_pattern'] = f'{image_path.stem}-[generation_number]'
|
|
|
|
else:
|
|
|
|
p.override_settings['samples_filename_pattern'] = f'{image_path.stem}'
|
2023-07-29 09:01:53 -06:00
|
|
|
process_images(p)
|
2022-09-22 03:11:48 -06:00
|
|
|
|
|
|
|
|
2023-08-12 08:46:13 -06:00
|
|
|
def img2img(id_task: str, mode: int, prompt: str, negative_prompt: str, prompt_styles, init_img, sketch, init_img_with_mask, inpaint_color_sketch, inpaint_color_sketch_orig, init_img_inpaint, init_mask_inpaint, steps: int, sampler_name: str, mask_blur: int, mask_alpha: float, inpainting_fill: int, n_iter: int, batch_size: int, cfg_scale: float, image_cfg_scale: float, denoising_strength: float, selected_scale_tab: int, height: int, width: int, scale_by: float, resize_mode: int, inpaint_full_res: bool, inpaint_full_res_padding: int, inpainting_mask_invert: int, img2img_batch_input_dir: str, img2img_batch_output_dir: str, img2img_batch_inpaint_mask_dir: str, override_settings_texts, img2img_batch_use_png_info: bool, img2img_batch_png_info_props: list, img2img_batch_png_info_dir: str, request: gr.Request, *args):
|
2023-01-29 16:40:26 -07:00
|
|
|
override_settings = create_override_settings_dict(override_settings_texts)
|
|
|
|
|
2023-01-11 10:33:24 -07:00
|
|
|
is_batch = mode == 5
|
|
|
|
|
|
|
|
if mode == 0: # img2img
|
2023-08-17 17:35:34 -06:00
|
|
|
image = init_img
|
2023-01-11 10:33:24 -07:00
|
|
|
mask = None
|
|
|
|
elif mode == 1: # img2img sketch
|
2023-08-17 17:35:34 -06:00
|
|
|
image = sketch
|
2023-01-11 10:33:24 -07:00
|
|
|
mask = None
|
|
|
|
elif mode == 2: # inpaint
|
|
|
|
image, mask = init_img_with_mask["image"], init_img_with_mask["mask"]
|
2023-08-15 12:24:55 -06:00
|
|
|
mask = processing.create_binary_mask(mask)
|
2023-01-11 10:33:24 -07:00
|
|
|
elif mode == 3: # inpaint sketch
|
|
|
|
image = inpaint_color_sketch
|
|
|
|
orig = inpaint_color_sketch_orig or inpaint_color_sketch
|
|
|
|
pred = np.any(np.array(image) != np.array(orig), axis=-1)
|
|
|
|
mask = Image.fromarray(pred.astype(np.uint8) * 255, "L")
|
|
|
|
mask = ImageEnhance.Brightness(mask).enhance(1 - mask_alpha / 100)
|
|
|
|
blur = ImageFilter.GaussianBlur(mask_blur)
|
|
|
|
image = Image.composite(image.filter(blur), orig, mask.filter(blur))
|
|
|
|
elif mode == 4: # inpaint upload mask
|
|
|
|
image = init_img_inpaint
|
|
|
|
mask = init_mask_inpaint
|
2022-09-03 03:08:45 -06:00
|
|
|
else:
|
2023-01-11 10:33:24 -07:00
|
|
|
image = None
|
2022-09-03 03:08:45 -06:00
|
|
|
mask = None
|
|
|
|
|
2022-10-25 02:39:59 -06:00
|
|
|
# Use the EXIF orientation of photos taken by smartphones.
|
2022-10-26 06:32:55 -06:00
|
|
|
if image is not None:
|
2022-11-08 20:06:29 -07:00
|
|
|
image = ImageOps.exif_transpose(image)
|
2022-10-25 02:39:59 -06:00
|
|
|
|
2023-05-29 10:47:20 -06:00
|
|
|
if selected_scale_tab == 1 and not is_batch:
|
2023-03-28 13:23:40 -06:00
|
|
|
assert image, "Can't scale by because no image is selected"
|
|
|
|
|
|
|
|
width = int(image.width * scale_by)
|
|
|
|
height = int(image.height * scale_by)
|
|
|
|
|
2022-09-03 03:08:45 -06:00
|
|
|
assert 0. <= denoising_strength <= 1., 'can only work with strength in [0.0, 1.0]'
|
|
|
|
|
|
|
|
p = StableDiffusionProcessingImg2Img(
|
|
|
|
sd_model=shared.sd_model,
|
|
|
|
outpath_samples=opts.outdir_samples or opts.outdir_img2img_samples,
|
|
|
|
outpath_grids=opts.outdir_grids or opts.outdir_img2img_grids,
|
|
|
|
prompt=prompt,
|
2022-09-09 00:15:36 -06:00
|
|
|
negative_prompt=negative_prompt,
|
2023-01-14 04:56:39 -07:00
|
|
|
styles=prompt_styles,
|
2023-08-08 12:28:34 -06:00
|
|
|
sampler_name=sampler_name,
|
2022-09-03 03:08:45 -06:00
|
|
|
batch_size=batch_size,
|
|
|
|
n_iter=n_iter,
|
|
|
|
steps=steps,
|
|
|
|
cfg_scale=cfg_scale,
|
|
|
|
width=width,
|
|
|
|
height=height,
|
|
|
|
init_images=[image],
|
|
|
|
mask=mask,
|
|
|
|
mask_blur=mask_blur,
|
|
|
|
inpainting_fill=inpainting_fill,
|
|
|
|
resize_mode=resize_mode,
|
|
|
|
denoising_strength=denoising_strength,
|
2023-02-03 16:19:56 -07:00
|
|
|
image_cfg_scale=image_cfg_scale,
|
2022-09-03 03:08:45 -06:00
|
|
|
inpaint_full_res=inpaint_full_res,
|
2022-09-22 03:11:48 -06:00
|
|
|
inpaint_full_res_padding=inpaint_full_res_padding,
|
2022-09-03 12:02:38 -06:00
|
|
|
inpainting_mask_invert=inpainting_mask_invert,
|
2023-01-29 16:40:26 -07:00
|
|
|
override_settings=override_settings,
|
2022-09-03 03:08:45 -06:00
|
|
|
)
|
2022-10-02 11:23:40 -06:00
|
|
|
|
2023-04-24 03:36:16 -06:00
|
|
|
p.scripts = modules.scripts.scripts_img2img
|
2022-10-22 03:23:45 -06:00
|
|
|
p.script_args = args
|
2022-10-16 09:53:56 -06:00
|
|
|
|
2023-06-14 13:03:44 -06:00
|
|
|
p.user = request.username
|
|
|
|
|
2022-10-02 11:23:40 -06:00
|
|
|
if shared.cmd_opts.enable_console_prompts:
|
|
|
|
print(f"\nimg2img: {prompt}", file=shared.progress_print_out)
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2023-03-28 13:20:31 -06:00
|
|
|
if mask:
|
|
|
|
p.extra_generation_params["Mask blur"] = mask_blur
|
2022-09-20 10:07:09 -06:00
|
|
|
|
2023-07-10 11:08:23 -06:00
|
|
|
with closing(p):
|
|
|
|
if is_batch:
|
|
|
|
assert not shared.cmd_opts.hide_ui_dir_config, "Launched with --hide-ui-dir-config, batch img2img disabled"
|
2022-09-24 07:29:20 -06:00
|
|
|
|
2023-07-10 11:08:23 -06:00
|
|
|
process_batch(p, img2img_batch_input_dir, img2img_batch_output_dir, img2img_batch_inpaint_mask_dir, args, to_scale=selected_scale_tab == 1, scale_by=scale_by, use_png_info=img2img_batch_use_png_info, png_info_props=img2img_batch_png_info_props, png_info_dir=img2img_batch_png_info_dir)
|
2022-09-03 08:21:15 -06:00
|
|
|
|
2023-07-10 11:08:23 -06:00
|
|
|
processed = Processed(p, [], p.seed, "")
|
|
|
|
else:
|
|
|
|
processed = modules.scripts.scripts_img2img.run(p, *args)
|
|
|
|
if processed is None:
|
|
|
|
processed = process_images(p)
|
2022-11-01 18:56:47 -06:00
|
|
|
|
2022-09-08 07:37:13 -06:00
|
|
|
shared.total_tqdm.clear()
|
2022-09-03 03:08:45 -06:00
|
|
|
|
2022-09-22 02:47:16 -06:00
|
|
|
generation_info_js = processed.js()
|
|
|
|
if opts.samples_log_stdout:
|
|
|
|
print(generation_info_js)
|
|
|
|
|
2022-10-04 08:23:48 -06:00
|
|
|
if opts.do_not_show_images:
|
|
|
|
processed.images = []
|
|
|
|
|
2023-07-14 23:07:25 -06:00
|
|
|
return processed.images, generation_info_js, plaintext_to_html(processed.info), plaintext_to_html(processed.comments, classname="comments")
|