Why can a drawn rectangle exceed a crop by one row and column?

rectangle includes both endpoint coordinates in the painted area.

Pixel experiment and interpretation

Painting from (1,1) to (2,2) fills four cells. Subtract one from the right and bottom edges when converting a half-open crop to inclusive pixel endpoints.

from PIL import Image, ImageDraw

im=Image.new('L',(4,4))
ImageDraw.Draw(im).rectangle((1,1,2,2),fill=255)
im.histogram()[255]

Result 4

中文

为什么画的矩形掩膜比裁剪框多一行一列?

rectangle 将两端坐标包含在绘制范围内。

像素实验与适用边界

从 (1,1) 到 (2,2) 会涂四格。将半开裁剪框用作像素标注前,右边和下边的终点需各减一。

from PIL import Image, ImageDraw

im=Image.new('L',(4,4))
ImageDraw.Draw(im).rectangle((1,1,2,2),fill=255)
im.histogram()[255]

结果 4

References / 参考来源

Keep reading / 继续阅读