Does a crop include the pixel at its lower-right corner?

Pillow measures a crop between four edges. Its width is the right edge minus the left edge.

Pixel experiment and interpretation

Treat the box as a paper cutter. Edges at 1 and 3 enclose two columns. Convert inclusive annotation endpoints before passing them in.

from PIL import Image

def row(values):
    return Image.frombytes('L', (len(values), 1), bytes(values))

im=row([10,20,30,40])
list(im.crop((1,0,3,1)).tobytes())

Result [20, 30]

中文

裁剪框的右下角也算一个像素吗?

Pillow 的裁剪框用左、上、右、下四条边表示,宽度等于右减左。

像素实验与适用边界

把标注框当作剪纸模板。横坐标从 1 到 3 的切口保留两列;若标注协议给的是最后一个像素的编号,先换成边界坐标。

from PIL import Image

def row(values):
    return Image.frombytes('L', (len(values), 1), bytes(values))

im=row([10,20,30,40])
list(im.crop((1,0,3,1)).tobytes())

结果 [20, 30]

References / 参考来源

Keep reading / 继续阅读