diff --git a/modules/masking.py b/modules/masking.py index 6f83c4ea4..3423f0071 100644 --- a/modules/masking.py +++ b/modules/masking.py @@ -80,19 +80,19 @@ def expand_crop_region(crop_region, processing_width, processing_height, image_w 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, processing_dimension, image_dimension): - """expands the segment given by c1 c2 to the desired_dimension but not exceeding the boundaries of the image_dimension""" - if (diff := processing_dimension + c1 - c2) > 0: - # if the region is smaller than processing_dimension, extend both sides equally + 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, image_dimension) # ensure c2 is within image_dimension + c2 = min(c2 - c1, maximal_coordinate) # ensure c2 is within maximal_coordinate c1 = 0 - elif c2 >= image_dimension: # shift the region to the left by (c2 - image_dimension) - c1 = max(c1 - c2 + image_dimension, 0) # ensure c1 is not below 0 - c2 = image_dimension + 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 @@ -100,7 +100,7 @@ def expand_too_small_crop_region(crop_region, processing_width, processing_heigh y1, y2 = _expand_segment(y1, y2, processing_height, image_height) new_crop_region = x1, y1, x2, y2 if new_crop_region != crop_region: - print(f"Crop region {crop_region} was smaller then process resolution and has been expanded to {new_crop_region}") + print("Crop region was smaller then resolution and has been corrected") return new_crop_region