I'm trying to understand the PIL getbbox (get boundary box) function, which "Calculates the bounding box of the non-zero regions in the image."
我正在尝试理解PIL getbbox(获取边界框)功能,即“计算图像中非零区域的边界框”。
In the following code, I use getbbox and it returns exactly what I expect it to with simple grayscale images.
在下面的代码中,我使用了getbbox,它使用简单的灰度图像返回了我期望的结果。
import numpy as np
from PIL import Image, ImageFont, ImageDraw, ImageEnhance,ImageChops
import PIL
def trim(im):
dr = ImageDraw.Draw(im)
bg = Image.new(im.mode, im.size, im.getpixel((0,0)))
diff = ImageChops.difference(im, bg)
bbox = diff.getbbox()
#None if getbbox returns None
if bbox:
return im.crop(bbox)
I don't however understand how it works accord to its source code. Besides the self.load(), it seems to do some sort of recursive call on itself. I thought recursive functions needed terminating conditions, but I see none?
但是,根据其源代码,我不了解它是如何工作的。除了self.load()之外,它似乎还在进行某种递归调用。我认为递归函数需要终止条件,但我没有看到?
I feel like it has something to do with somehow removing the black pixels in the image array, but I just don't understand these seemingly cryptic 2 lines of code (below). Any help in me understanding this would be appreciated.
我觉得它与以某种方式删除图像阵列中的黑色像素有关,但我只是不明白这些看似神秘的2行代码(下图)。任何帮助我理解这一点将不胜感激。
def getbbox(self):
"""
Calculates the bounding box of the non-zero regions in the
image.
:returns: The bounding box is returned as a 4-tuple defining the
left, upper, right, and lower pixel coordinate. If the image
is completely empty, this method returns None.
"""
self.load()
return self.im.getbbox()
edit: pasted in source code
编辑:粘贴在源代码中
1 个解决方案
#1
0
As has been commented, this is not recursion. im.getbbox() calls https://github.com/python-pillow/Pillow/blob/master/src/_imaging.c#L1947
正如已经评论过的,这不是递归。 im.getbbox()调用https://github.com/python-pillow/Pillow/blob/master/src/_imaging.c#L1947
That then calls ImagingGetBBox - https://github.com/python-pillow/Pillow/blob/master/src/libImaging/GetBBox.c#L24
然后调用ImagingGetBBox - https://github.com/python-pillow/Pillow/blob/master/src/libImaging/GetBBox.c#L24
#1
0
As has been commented, this is not recursion. im.getbbox() calls https://github.com/python-pillow/Pillow/blob/master/src/_imaging.c#L1947
正如已经评论过的,这不是递归。 im.getbbox()调用https://github.com/python-pillow/Pillow/blob/master/src/_imaging.c#L1947
That then calls ImagingGetBBox - https://github.com/python-pillow/Pillow/blob/master/src/libImaging/GetBBox.c#L24
然后调用ImagingGetBBox - https://github.com/python-pillow/Pillow/blob/master/src/libImaging/GetBBox.c#L24