Created
November 2, 2020 05:56
-
-
Save sharkdeng/f3f2b4dd8d2b6a78f010c548624bcc3d to your computer and use it in GitHub Desktop.
crop white background of an image
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
def crop_white(image: np.ndarray, value: int = 255) -> np.ndarray: | |
assert image.shape[2] == 3 | |
assert image.dtype == np.uint8 | |
ys, = (image.min((1, 2)) < value).nonzero() | |
xs, = (image.min(0).min(1) < value).nonzero() | |
if len(xs) == 0 or len(ys) == 0: | |
return image | |
return image[ys.min():ys.max() + 1, xs.min():xs.max() + 1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment