I think it should be a very simple problem, but I cannot find a solution or an effective keyword for search.
I just have this image.
I thought this answer is much more succinct:
def crop(image): y_nonzero, x_nonzero, _ = np.nonzero(image) return image[np.min(y_nonzero):np.max(y_nonzero), np.min(x_nonzero):np.max(x_nonzero)]