Cropping binary images based on pixel counts in python
One approach that is always available for binary images are morphological operations.

The critical piece of code is
# from skimage import morphology
# import numpy as np

# filter elements smaller than 1/6-th of the total width
selem = morphology.rectangle(1, img_binary.shape[1] // 6)
img_opened = morphology.opening(img_binary, selem=selem)

hist = np.sum(img_opened, axis=1)
non_zero = np.argwhere(hist &gt; 0)
top_bound = int(np.round(np.min(non_zero) * (1 - 0.05))) # 5% tolerance
bottom_bound = int(np.round(np.max(non_zero) * (1 + 0.05))) # 5% tolerance

Here is the notebook I used to produce the image above:
https://gist.github.com/FirefoxMetzger/5d0d5e207bf8182f61374ac9ad80f10f