Merge b2bbc01b920085d089c03ed5a787f56b70fd7773 into 2174ce5afea90ca489d222f539988dcef59f1027

This commit is contained in:
w-e-w 2025-05-04 08:20:14 +00:00 committed by GitHub
commit 7a49a3196f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 0 deletions

View File

@ -77,6 +77,33 @@ def expand_crop_region(crop_region, processing_width, processing_height, image_w
return x1, y1, x2, y2 return x1, y1, x2, y2
def expand_too_small_crop_region(crop_region, processing_width, processing_height, image_width, image_height):
"""expands a crop_region to not have dimensions smaller than processing_dimensions"""
def _expand_segment(c1, c2, desirable_length, maximal_coordinate):
"""expands the segment given by c1 c2 to the desired_dimension but not exceeding the boundaries of the maximal_coordinate"""
if (diff := desirable_length + c1 - c2) > 0:
# if the region is smaller than desirable_length, extend both sides equally
diff_l = diff // 2
c1 -= diff_l
c2 += diff - diff_l
if c1 < 0: # shift the region to the right by c1
c2 = min(c2 - c1, maximal_coordinate) # ensure c2 is within maximal_coordinate
c1 = 0
elif c2 >= maximal_coordinate: # shift the region to the left by (c2 - maximal_coordinate)
c1 = max(c1 - c2 + maximal_coordinate, 0) # ensure c1 is not below 0
c2 = maximal_coordinate
return c1, c2
x1, y1, x2, y2 = crop_region
x1, x2 = _expand_segment(x1, x2, processing_width, image_width)
y1, y2 = _expand_segment(y1, y2, processing_height, image_height)
new_crop_region = x1, y1, x2, y2
if new_crop_region != crop_region:
print("Crop region was smaller then resolution and has been corrected")
return new_crop_region
def fill(image, mask): def fill(image, mask):
"""fills masked regions with colors from image using blur. Not extremely effective.""" """fills masked regions with colors from image using blur. Not extremely effective."""

View File

@ -1642,6 +1642,8 @@ class StableDiffusionProcessingImg2Img(StableDiffusionProcessing):
crop_region = masking.get_crop_region_v2(mask, self.inpaint_full_res_padding) crop_region = masking.get_crop_region_v2(mask, self.inpaint_full_res_padding)
if crop_region: if crop_region:
crop_region = masking.expand_crop_region(crop_region, self.width, self.height, mask.width, mask.height) crop_region = masking.expand_crop_region(crop_region, self.width, self.height, mask.width, mask.height)
if shared.opts.forbid_too_small_crop_region:
crop_region = masking.expand_too_small_crop_region(crop_region, self.width, self.height, mask.width, mask.height)
x1, y1, x2, y2 = crop_region x1, y1, x2, y2 = crop_region
mask = mask.crop(crop_region) mask = mask.crop(crop_region)
image_mask = images.resize_image(2, mask, self.width, self.height) image_mask = images.resize_image(2, mask, self.width, self.height)

View File

@ -228,6 +228,7 @@ options_templates.update(options_section(('img2img', "img2img", "sd"), {
"return_mask_composite": OptionInfo(False, "For inpainting, include masked composite in results for web"), "return_mask_composite": OptionInfo(False, "For inpainting, include masked composite in results for web"),
"img2img_batch_show_results_limit": OptionInfo(32, "Show the first N batch img2img results in UI", gr.Slider, {"minimum": -1, "maximum": 1000, "step": 1}).info('0: disable, -1: show all images. Too many images can cause lag'), "img2img_batch_show_results_limit": OptionInfo(32, "Show the first N batch img2img results in UI", gr.Slider, {"minimum": -1, "maximum": 1000, "step": 1}).info('0: disable, -1: show all images. Too many images can cause lag'),
"overlay_inpaint": OptionInfo(True, "Overlay original for inpaint").info("when inpainting, overlay the original image over the areas that weren't inpainted."), "overlay_inpaint": OptionInfo(True, "Overlay original for inpaint").info("when inpainting, overlay the original image over the areas that weren't inpainted."),
"forbid_too_small_crop_region": OptionInfo(False, "Forbid too small crop region").info("Correct inpaint padding for only masked to avoid crop region sides less then processing resolution"),
})) }))
options_templates.update(options_section(('optimizations', "Optimizations", "sd"), { options_templates.update(options_section(('optimizations', "Optimizations", "sd"), {