Skip to content

Commit

Permalink
rename variables and print coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
w-e-w committed Oct 31, 2024
1 parent 884f1dc commit b2bbc01
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions modules/masking.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,27 @@ 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
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(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


Expand Down

0 comments on commit b2bbc01

Please sign in to comment.