简体   繁体   中英

show coordinates on plot using PIL

I'm sorry, I can't seem to find how to do this. This works, but I would love to have the coordinates plotted along the axis:

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

在此处输入图片说明

Does anyone have any advice? Just would love to see numbers along the vertical and horizontal components of the plot. Thanks.

Here is a "brute force" way to do this. You could generalize it to better handle different x/y ranges. Maybe you could use matplotlib and potentially go off 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

非常基本的例子

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM