根据文献资料,枕头的坐标系如下:
Python图像库使用笛卡尔像素坐标系,左上角为(0,0)。注意,坐标指的是隐含的像素角;一个像素(0,0)的中心实际上位于(0.5,0.5)。 坐标通常以2元组(x,y)的形式传递给库。矩形表示为4元组,左上角先给出.例如,覆盖所有800x600像素图像的矩形被写为(0,0,800,600)。
https://pillow.readthedocs.io/en/stable/handbook/concepts.html#coordinate-system
对于图像操作部分(命令如裁剪和粘贴),这是正确的,但是绘图部分使用另一个坐标系统,尽管文档声明它是相同的:
图形界面使用与PIL本身相同的坐标系,左上角为(0,0)。
https://pillow.readthedocs.io/en/stable/reference/ImageDraw.html#concepts
例如,如果我运行这个程序:
from PIL import Image
from PIL import ImageDraw
NewPhoto = Image.new('RGB', (16, 16), 'white')
Draw = ImageDraw.Draw(NewPhoto)
Draw.rectangle((4, 4, 12, 12), fill='gray')
for X in range(3, 14, 2):
Draw.line((X, 5, X, 8), fill='black', width=1)
NewPhoto.show()我得到一个9x9矩形,而不是预期的8x8长方形(看这张照片)。我在MacOS 10.13.6上运行Python3.8,使用枕头6.2.0。
另一个例子是:
from PIL import Image
from PIL import ImageDraw
NewPhoto = Image.new('RGB', (16, 16), 'white')
Draw = ImageDraw.Draw(NewPhoto)
Draw.rectangle((4, 4, 12, 12), fill='gray')
box = (4, 4, 13, 13)
region = NewPhoto.crop(box)
NewPhoto2 = Image.new('RGB', (16, 16), 'red')
NewPhoto2.paste(region, box)
NewPhoto2.show()枕头的作物和粘贴功能确实如广告所示,但矩形(4、4、12、12)太大,完全填满作物区域(4、4、13、13)。
是我做错了什么,文档是错的,还是有其他问题可以解释这种差异?
发布于 2019-11-10 20:17:10
正如这里所解释的,Draw.rectangle的第一个参数是四个角点。X坐标4和12之间有9列(两端都包含在内),因此矩形宽9像素。高度也是一样的。
https://stackoverflow.com/questions/58792202
复制相似问题