Python 有没有办法在PIL中指定矩形的宽度?

Python 有没有办法在PIL中指定矩形的宽度?,python,python-imaging-library,draw,Python,Python Imaging Library,Draw,我正在尝试使用PIL/Pizz的ImageDraw模块在图像上绘制厚矩形 我尝试使用draw.rectangle([x1,y1,x2,y2],outline='yellow',width=3)但它似乎不喜欢width参数 我可以用一堆行来模拟我想做的事情,但我想知道是否有一种合适的方法来做 ''' coordinates = [(x1, y1), (x2, y2)] (x1, y1) *-------------- | |

我正在尝试使用PIL/Pizz的ImageDraw模块在图像上绘制厚矩形

我尝试使用
draw.rectangle([x1,y1,x2,y2],outline='yellow',width=3)
但它似乎不喜欢width参数

我可以用一堆行来模拟我想做的事情,但我想知道是否有一种合适的方法来做

'''
coordinates = [(x1, y1), (x2, y2)]

    (x1, y1)
        *--------------
        |             |
        |             |
        |             |
        |             |
        |             |
        |             |
        --------------*
                      (x2, y2)

'''

def draw_rectangle(drawing, xy, outline='yellow', width=10):
    top_left = xy[0]
    bottom_right = xy[1]
    top_right = (xy[1][0], xy[0][1])
    bottom_left= (xy[0][0], xy[1][1])

    drawing.line([top_left, top_right], fill=outline, width=width)
    drawing.line([top_right, bottom_right], fill=outline, width=width)
    drawing.line([bottom_right, bottom_left], fill=outline, width=width)
    drawing.line([bottom_left, top_left], fill=outline, width=width)

更新-枕头>=5.3.0
矩形
现在支持
宽度
参数:
PIL.ImageDraw.ImageDraw.rectangle(xy,fill=None,outline=None,width=0)

先前的答案:

这是一种绘制第一个初始矩形,然后进一步向内绘制矩形的方法-注意,线宽不是沿边框居中的

def draw_rectangle(draw, coordinates, color, width=1):
    for i in range(width):
        rect_start = (coordinates[0][0] - i, coordinates[0][1] - i)
        rect_end = (coordinates[1][0] + i, coordinates[1][1] + i)
        draw.rectangle((rect_start, rect_end), outline = color)

# example usage

im = Image.open(image_path)
drawing = ImageDraw.Draw(im)

top_left = (50, 50)
bottom_right = (100, 100)

outline_width = 10
outline_color = "black"

draw_rectangle(drawing, (top_left, bottom_right), color=outline_color, width=outline_width)

您可以绘制视图矩形,例如:

draw.rectangle([(x, y),(x+w,y+h) ], outline=(0,0,255,255))
draw.rectangle([(x+1, y+1),(x+w-1,y+h-1) ], outline=(0,0,255,255))
draw.rectangle([(x+2, y+2),(x+w-2,y+h-2) ], outline=(0,0,255,255))
...

当然,在一个循环和一个函数中。

您也可以用四个点画一条线,形成一个矩形,而不是四条线:

def drawrect(drawcontext,xy,outline=None,width=0):
(x1,y1),(x2,y2)=xy
点=(x1,y1),(x2,y1),(x2,y2),(x1,y2),(x1,y1)
drawcontext.line(点,填充=轮廓,宽度=宽度)
#范例
从PIL导入图像,ImageDraw
im=图像。新(“RGB”(150150),color=“白色”)
draw=ImageDraw.draw(im)
drawrect(draw,[(50,50),(100,100)],outline=“红色”,width=5)
im.show()

此方法适用于Ubuntu 18.04上的PIL v1.1.7和pillow v5.3.0。角点不是正方形,但至少只关闭1/2像素,而不是使用宽度参数绘制4条线来创建矩形,每个角点至少关闭1像素。我认为pillow/pil中的线条绘制算法仍然存在缺陷

def drawrect(drawcontext, xy, color=None, width=1):
    (x1, y1), (x2, y2) = xy
    offset = 1
    for i in range(0, width):
        drawcontext.rectangle(((x1, y1), (x2, y2)), outline=color)
        x1 = x1 - offset
        y1 = y1 + offset
        x2 = x2 + offset
        y2 = y2 - offset
然后在代码中,您将有:

# example
from PIL import Image, ImageDraw
im = Image.new("RGB", (150, 150), color="white")
draw = ImageDraw.Draw(im)

drawrect(draw, [(50, 50), (100, 100)], outline="red", width=5)
del draw
im.show()

感谢gdwarf的解决方案,让我走上了这条路

PIL矩形现在支持
width
参数

from PIL import Image, ImageDraw

height = width = 800
img = Image.new('RGB', (height, width), (255, 255, 255))
draw = ImageDraw.Draw(img)
draw.rectangle([100,100,500,400], width = 10, outline="#0000ff")
img.show()

我使用的是PIL v1.1.7和pillow v5.3.0,并绘制了一个带有线条的矩形,它的角部缺少像素。我认为在画线的算法中仍然有一个缺陷。更好的方法是在循环中绘制尺寸不断增大的矩形,以近似宽度参数。见下面我的答案。