PIL 中图像边框颜色的问题 [英] Issue with border color of image in PIL

查看:46
本文介绍了PIL 中图像边框颜色的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的 PIL 图像有问题.似乎灰色背景的边框边缘采用了绘制在背景之上的椭圆的颜色.

I'm having an issue with a PIL image that I'm creating. It seems as if the border edges of the grey background is taking the colour of the ellipse drawn on top of the background.

首先,我使用辅助函数通过使用抗锯齿来使椭圆看起来更平滑.但是,我认为这个辅助函数中的掩码导致了一个问题,它在灰色背景的每个角落留下了一个非常小的(绿色)轮廓.

Firstly, I'm using a helper function to allow for a smoother looking ellipse by using antialias. However, I think the mask in this helper function is causing an issue where it's leaving an very small (green) outlined in each corner of the of the gray background.

对于我的角落,我使用另一个辅助函数来创建这些.也许这里的两个功能有一些冲突,但我不太确定.

For my corners I'm using another helper function to create these. Maybe there is some confliction with both functions here but I'm not too sure.

这是我想要做的:

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 ( -7, 'white' ), ( width, '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

def add_corners(im, rad):
    circle = Image.new('L', (rad * 2, rad * 2), 0)
    draw = ImageDraw.Draw(circle)
    draw.ellipse((0, 0, rad * 2, rad * 2), fill=255)
    alpha = Image.new('L', im.size, 255)
    w, h = im.size
    alpha.paste(circle.crop((0, 0, rad, rad)), (0, 0))
    alpha.paste(circle.crop((0, rad, rad, rad * 2)), (0, h - rad))
    alpha.paste(circle.crop((rad, 0, rad * 2, rad)), (w - rad, 0))
    alpha.paste(circle.crop((rad, rad, rad * 3, rad * 3)), (w - rad, h - rad))
    im.putalpha(alpha)
    return im

    #Grey background
    im = Image.new("RGBA", (900, 296), (44, 44, 44, 255))
    #Add some corners
    im = add_corners(im, 50) 
    im_draw = ImageDraw.Draw(im)


    #Green Ellipse
    ellipse_box = [55, 37, 107 + 48 + 46, 103 + 80]
    draw_ellipse(im, ellipse_box, width=20, outline=(52, 235, 52))

    im.save("image.png")

输出:

推荐答案

im = add_corners(im, 50) 

这部分代码应该在后面

ellipse_box = [55, 37, 107 + 48 + 46, 103 + 80]
draw_ellipse(im, ellipse_box, width=20, outline=(52, 235, 52))

这解决了问题.

这篇关于PIL 中图像边框颜色的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆