-
Notifications
You must be signed in to change notification settings - Fork 26.6k
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
improve get_crop_region #14709
Merged
Merged
improve get_crop_region #14709
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
w-e-w
force-pushed
the
improve-get_crop_region
branch
from
January 20, 2024 22:58
3efbfd2
to
5f13560
Compare
w-e-w
force-pushed
the
improve-get_crop_region
branch
5 times, most recently
from
January 20, 2024 23:30
3ed1149
to
cc1e7e7
Compare
it can be compacted down even more at the cost of readability def get_crop_region(mask, pad=0):
mask_img = mask if isinstance(mask, Image.Image) else Image.fromarray(mask)
box = mask_img.getbbox()
return max((box[0] - pad, 0), max(box[1] - pad, 0), min(box[2] + pad, mask_img.size[0]), max(box[3] + pad, mask_img.size[1])) if box else (min(mask_img.size[0] - pad, 0), min(mask_img.size[1] - pad, 0), max(0 + pad, mask_img.size[0]), max(0 + pad, mask_img.size[1])) def stupid(mask, pad=0):
mask_img = mask if isinstance(mask, Image.Image) else Image.fromarray(mask)
return (max(box[0] - pad, 0), max(box[1] - pad, 0), min(box[2] + pad, mask_img.size[0]), min(box[3] + pad, mask_img.size[1])) if (box := mask_img.getbbox()) else (max(mask_img.size[0] - pad, 0), max(mask_img.size[1] - pad, 0), min(pad, mask_img.size[0]), min(pad, mask_img.size[1])) def shouldnt_ever_exist(mask, pad=0):
return (max(box[0] - pad, 0), max(box[1] - pad, 0), min(box[2] + pad, mask_img.size[0]), min(box[3] + pad, mask_img.size[1])) if (box := (mask_img := mask if isinstance(mask, Image.Image) else Image.fromarray(mask)).getbbox()) else (max(mask_img.size[0] - pad, 0), max(mask_img.size[1] - pad, 0), min(pad, mask_img.size[0]), min(pad, mask_img.size[1])) def just_why(mask, pad=0): return (max(box[0] - pad, 0), max(box[1] - pad, 0), min(box[2] + pad, mask_img.size[0]), min(box[3] + pad, mask_img.size[1])) if (box := (mask_img := mask if isinstance(mask, Image.Image) else Image.fromarray(mask)).getbbox()) else (max(mask_img.size[0] - pad, 0), max(mask_img.size[1] - pad, 0), min(pad, mask_img.size[0]), min(pad, mask_img.size[1])) lambda_version = lambda mask, pad=0: (max(box[0] - pad, 0), max(box[1] - pad, 0), min(box[2] + pad, mask_img.size[0]), min(box[3] + pad, mask_img.size[1])) if (box := (mask_img := mask if isinstance(mask, Image.Image) else Image.fromarray(mask)).getbbox()) else (max(mask_img.size[0] - pad, 0), max(mask_img.size[1] - pad, 0), min(pad, mask_img.size[0]), min(pad, mask_img.size[1])) c = lambda m, p=0: (max(b[0] - p, 0), max(b[1] - p, 0), min(b[2] + p, i.size[0]), min(b[3] + p, i.size[1])) if (b := (i := m if isinstance(m, Image.Image) else Image.fromarray(m)).getbbox()) else (max(i.size[0] - p, 0), max(i.size[1] - p, 0), min(p, i.size[0]), min(p, i.size[1])) |
w-e-w
force-pushed
the
improve-get_crop_region
branch
from
January 21, 2024 00:02
cc1e7e7
to
e36827a
Compare
AUTOMATIC1111
approved these changes
Jan 21, 2024
Closed
Closed
4 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
simplify and improve get_crop_region
test code
performance comparison code
conclusion
all round faster even in compatibility mode
Checklist: