Skip to content

Commit

Permalink
add "Forbid too small crop region" option
Browse files Browse the repository at this point in the history
  • Loading branch information
light-and-ray committed Oct 28, 2024
1 parent 9f5a98d commit 11971e8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
38 changes: 38 additions & 0 deletions modules/masking.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,44 @@ def expand_crop_region(crop_region, processing_width, processing_height, image_w
return x1, y1, x2, y2


def expand_too_small_crop_region(crop_region, processing_width, processing_height, image_width, image_height):
"""expands crop region to not have width and height smaller then processing_width and processing_height"""

x1, y1, x2, y2 = crop_region

desired_w = processing_width
diff_w = desired_w - (x2 - x1)
if diff_w > 0:
diff_w_l = diff_w // 2
diff_w_r = diff_w - diff_w_l
x1 -= diff_w_l
x2 += diff_w_r
if x1 < 0:
x2 -= x1
x1 -= x1
if x2 >= image_width:
x2 = image_width

desired_h = processing_height
diff_h = desired_h - (y2 - y1)
if diff_h > 0:
diff_h_u = diff_h // 2
diff_h_d = diff_h - diff_h_u
y1 -= diff_h_u
y2 += diff_h_d
if y1 < 0:
y2 -= y1
y1 -= y1
if y2 >= image_height:
y2 = image_height

if diff_h > 0 or diff_w > 0:
print("Crop region was smaller then resolution and has been corrected")

return x1, y1, x2, y2



def fill(image, mask):
"""fills masked regions with colors from image using blur. Not extremely effective."""

Expand Down
2 changes: 2 additions & 0 deletions modules/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1639,6 +1639,8 @@ def init(self, all_prompts, all_seeds, all_subseeds):
crop_region = masking.get_crop_region_v2(mask, self.inpaint_full_res_padding)
if crop_region:
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
mask = mask.crop(crop_region)
image_mask = images.resize_image(2, mask, self.width, self.height)
Expand Down
1 change: 1 addition & 0 deletions modules/shared_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@
"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'),
"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"), {
Expand Down

0 comments on commit 11971e8

Please sign in to comment.