def filter_margin(self, img):
"""Filter to remove empty margins in an image."""
# This filter is based on a simple Gaussian with a threshold
_img = ImageOps.invert(img.convert(mode='L'))
_img = _img.filter(ImageFilter.GaussianBlur(radius=3))
_img = _img.point(lambda x: (x >= 16) and x)
# If the image is white, we do not have bbox
return img.crop(self.bbox(_img)) if _img.getbbox() else img
评论列表
文章目录