使用线条粗细在Python PIL中绘制椭圆

ede*_*esz 12 python python-imaging-library python-2.7

我正在尝试使用Python在图像上绘制一个圆圈.我尝试使用PIL,但我想指定一个linewidth.目前,PIL绘制一个圆圈,但边界太薄.

这就是我所做的.

对于测试图像:我在MS Paint中创建了一个1632 X 1200图像并将其填充为绿色.我叫它test_1.jpg.这是输入文件: 输入

from PIL import Image, ImageDraw

im = Image.open('test_1.jpg')

width, height = im.size
eX, eY = 816,816 #Size of Bounding Box for ellipse

bbox =  (width/2 - eX/2, height/2 - eY/2, width/2 + eX/2, height/2 + eY/2)

draw = ImageDraw.Draw(im)
bbox_L = []
for j in range(0,5):
    bbox_L.append([element+j for element in bbox])
    draw.ellipse(tuple(bbox_L[j]), outline ='white')

im.show()
Run Code Online (Sandbox Code Playgroud)

基本上,我试图绘制多个圆圈,这些圆圈将在同一个点上居中但具有不同的半径.我的想法是,这会产生更粗的线条效果.

但是,这会产生下面附件中显示的输出: 产量

问题:正如您所看到的,左下角和右上角太薄了.此外,各个圆圈之间也存在间隙(参见左上角和右下角).

圆圈的厚度各不相同.我正在寻找一个厚度均匀的圆圈.

问题: 有没有办法在Python上绘制一个圆圈,在像test_1.jpg这样的图像上,使用PIL,NumPy等来指定线条厚度

Håk*_*Lid 11

我有同样的问题,并决定编写一个类似于你的辅助函数.此功能在遮罩层上绘制黑色和白色的两个同心椭圆,并通过遮罩将预期的轮廓颜色标记在原始图像上.为了获得更平滑的结果(抗锯齿),以更高的分辨率绘制椭圆和蒙版.

输出带或不带抗锯齿

PIL的椭圆

白色椭圆宽20像素,黑椭圆宽0.5像素.

from PIL import Image, ImageDraw

def draw_ellipse(image, bounds, width=1, outline='white', antialias=4):
    """Improved ellipse drawing function, based on PIL.ImageDraw."""

    # Use a single channel image (mode='L') as mask.
    # The size of the mask can be increased relative to the imput image
    # to get smoother looking results. 
    mask = Image.new(
        size=[int(dim * antialias) for dim in image.size],
        mode='L', color='black')
    draw = ImageDraw.Draw(mask)

    # draw outer shape in white (color) and inner shape in black (transparent)
    for offset, fill in (width/-2.0, 'white'), (width/2.0, 'black'):
        left, top = [(value + offset) * antialias for value in bounds[:2]]
        right, bottom = [(value - offset) * antialias for value in bounds[2:]]
        draw.ellipse([left, top, right, bottom], fill=fill)

    # downsample the mask using PIL.Image.LANCZOS 
    # (a high-quality downsampling filter).
    mask = mask.resize(image.size, Image.LANCZOS)
    # paste outline color to input image through the mask
    image.paste(outline, mask=mask)

# green background image
image = Image.new(mode='RGB', size=(700, 300), color='green')

ellipse_box = [50, 50, 300, 250]

# draw a thick white ellipse and a thin black ellipse
draw_ellipse(image, ellipse_box, width=20)

# draw a thin black line, using higher antialias to preserve finer detail
draw_ellipse(image, ellipse_box, outline='black', width=.5, antialias=8)

# Lets try without antialiasing
ellipse_box[0] += 350 
ellipse_box[2] += 350 

draw_ellipse(image, ellipse_box, width=20, antialias=1)
draw_ellipse(image, ellipse_box, outline='black', width=1, antialias=1)

image.show()
Run Code Online (Sandbox Code Playgroud)

我只在python 3.4中测试过这段代码,但我认为它应该可以在没有重大修改的情况下使用2.7.

  • 对于宽度 > 1,我发现需要进行以下更改以使椭圆完全适合边框:`对于偏移,填充 (0, 'white'), (width, 'black'):`。 (2认同)

mat*_*usc 5

一种简单(但不好用)的解决方案是绘制两个圆(较小的一个带有背景色):

outline = 10 # line thickness
draw.ellipse((x1-outline, y1-outline, x2+outline, y2+outline), fill=outline_color)
draw.ellipse((x1, y1, x2, y2), fill=background_color)
Run Code Online (Sandbox Code Playgroud)