-
Notifications
You must be signed in to change notification settings - Fork 27.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
alternative implantation of "Forbid too small crop region" #16617
base: dev
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,6 +77,33 @@ 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 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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. := is less readable |
||
# if the region is smaller than processing_dimension, 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 | ||
c1 = 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
imo
can't agree, I think min an max makes code cleaner There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Imo There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. still not convinced 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"""
diff = desirable_length + c1 - c2
if diff > 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 -= c1
c1 = 0
if c2 > maximal_coordinate:
c2 = maximal_coordinate
elif c2 >= maximal_coordinate: # shift the region to the left by (c2 - maximal_coordinate)
c1 -= c2 - maximal_coordinate
c2 = maximal_coordinate
if c1 < 0:
c1 = 0
return c1, c2
x1, y1, x2, y2 = crop_region |
||
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 | ||
w-e-w marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this new data is useful in the console output. Crop region is an internal variable. For user could be more useful new padding value, but in this case it can be different for w and h. I thought about it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to be honest I wasn't considering if it was useful to the user do you think is notified user about the adjustment of crop region even useful in the first place? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's useful because a person can not understand why their generation in the preview looks like padding is too big although they set it small, and so it can confuse them, so this message should be. But with no cropping region debugging info |
||
return new_crop_region | ||
|
||
|
||
def fill(image, mask): | ||
"""fills masked regions with colors from image using blur. Not extremely effective.""" | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awful naming. They are not dimensions, they are lengths. And your function is
_expand_segment
, how segment can have processing and image.processing_dimension
->desirable_length
,image_dimension
->maximal_coordinate
can be betterThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not to happy with the names myself
as always bad at naming things
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm pretty sure that dimensions is used to refer to the collective of width and height
dimension is refers to any side
width and height are specific sides
the variable names is a bit strange length seem to be better
maximal_coordinate not too sure