如何使用pil使用白色背景(透明?)的round_corner标识?

bdi*_*tor 9 python transparent rounded-corners python-imaging-library

我有一个方形徽标,我需要round_corner它,搜索了一会儿,并得到以下代码"工作":

def round_corner_jpg(image, radius):
    """generate round corner for image"""
    mask = Image.new('RGB', image.size)
    #mask = Image.new('RGB', (image.size[0] - radius, image.size[1] - radius))
    #mask = Image.new('L', image.size, 255)
    draw = aggdraw.Draw(mask)
    brush = aggdraw.Brush('black')
    width, height = mask.size
    draw.rectangle((0,0,width,height), aggdraw.Brush('white'))
    #upper-left corner
    draw.pieslice((0,0,radius*2, radius*2), 90, 180, None, brush)
    #upper-right corner
    draw.pieslice((width - radius*2, 0, width, radius*2), 0, 90, None, brush)
    #bottom-left corner
    draw.pieslice((0, height - radius * 2, radius*2, height),180, 270, None, brush)
    #bottom-right corner
    draw.pieslice((width - radius * 2, height - radius * 2, width, height), 270, 360, None, brush)
    #center rectangle
    draw.rectangle((radius, radius, width - radius, height - radius), brush)
    #four edge rectangle
    draw.rectangle((radius, 0, width - radius, radius), brush)
    draw.rectangle((0, radius, radius, height-radius), brush)
    draw.rectangle((radius, height-radius, width-radius, height), brush)
    draw.rectangle((width-radius, radius, width, height-radius), brush)
    draw.flush()
    del draw
    return ImageChops.add(mask, image)
Run Code Online (Sandbox Code Playgroud)

然后我保存了返回的图像对象,但它在角落里有白色背景, 这样 我怎么能摆脱白色背景或让它看不见?提前谢谢〜

编辑:这是fraxel的代码,谢谢〜

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, "white")
    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 * 2, rad * 2)), (w - rad, h - rad))
    im.putalpha(alpha)
    return im


if __name__ == '__main__':
    im = Image.open('1.jpg')
    im = add_corners(im, 100)
    im.save('out.png')`
Run Code Online (Sandbox Code Playgroud)

我很抱歉..我需要图像形状为椭圆形而不是矩形,IE写下图片中的东西,而@fraxel,我仍然可以看到你为我处理的图片中的白色角落

fra*_*xel 21

首先,确保以支持透明度的格式保存图像.PNG确实,JPG不...以下是一些非常好的代码,它将添加透明的角落.它的工作原理如下:

  1. 绘制一个半径的圆,rad使用draw.ellipse()
  2. 为与图像大小相同的Alpha通道创建图像
  3. 将我们的圆圈切成四个部分(圆角),并将它们放在alpha图像的正确角落
  4. 使用将alpha通道放入图像 putalpha()
  5. 保存为a png,从而保持透明度.

这是代码:

import Image, ImageDraw

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 * 2, rad * 2)), (w - rad, h - rad))
    im.putalpha(alpha)
    return im

im = Image.open('tiger.jpg')
im = add_corners(im, 100)
im.save('tiger.png')
Run Code Online (Sandbox Code Playgroud)

例如弯边虎:

在此输入图像描述

这是您的图像,使用此代码处理,提供透明角落:

在此输入图像描述