使用PIL在地块上显示坐标

时间:2018-10-03 00:38:55

标签: python python-imaging-library coordinate-systems

对不起,我似乎找不到解决方法。这行得通,但我希望沿轴绘制坐标:

from PIL import Image, ImageDraw
im = Image.new('RGBA', (250, 250), "white")
draw = ImageDraw.Draw(im)
draw.rectangle([(0, 0), (249, 249)], outline='black')  # just here to create a visible box
draw.rectangle([(10, 40), (100, 200)], fill='red', outline='red')
im

enter image description here

有人有什么建议吗?只是希望看到沿图的垂直和水平方向的数字。谢谢。

1 个答案:

答案 0 :(得分:2)

这是一种“强力”方式。您可以将其概括化以更好地处理不同的x / y范围。也许您可以使用matplotlib并可能退出this example

from PIL import Image, ImageDraw
im = Image.new('RGBA', (250, 250), "white")
draw = ImageDraw.Draw(im)
draw.rectangle([(0, 0), (249, 249)], outline='black')  # just here to create a visible box
draw.rectangle([(10, 40), (100, 200)], fill='red', outline='red')
# Draw x ticks
[draw.line(((x,250),(x,245)),fill='black') for x in range(0,249,5)]
# Draw x labels
[draw.text((x,235),str(x),fill='black')for x in range(0,249,25)]
# Can do same for y...
im

Very basic example