如何在没有白色背景(透明?)的圆角上使用pil?

时间:2012-07-02 03:09:45

标签: python python-imaging-library transparent rounded-corners

我有一个方形徽标,我需要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)

然后我保存了返回的图像对象,但它在角落里有白色背景 like this 我怎样才能摆脱白色背景或使其隐形? 提前谢谢〜

编辑: 这是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')`

我很抱歉..我需要图像形状为椭圆形而非矩形,I.E。写下来的照片和@fraxel,我仍然可以看到你为我处理的照片中的白角

2 个答案:

答案 0 :(得分:19)

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

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

    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')
    

    示例弯边虎:

    enter image description here

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

    enter image description here

答案 1 :(得分:1)

您是否尝试使用image.putalpha(mask)之类的内容替换图像的alpha通道?这似乎应该做你想要的。 mask可能必须处于“L”模式才能正常工作,而image应该是'RGBA',但可能会自动为您转换。

这里的最佳答案提供了一些很好的例子: How do I generate circular thumbnails with PIL?

你的面具图像看起来很好,但我认为你想要交换'白色'和'黑色',所以你有一个白色的圆角矩形,正好是你想要最终图像的形状,在黑色背景上。您可能还需要使用“L”模式(灰度)模式。

获得此图片后,您可以将return ImageChops.add(mask, image)替换为image.putalpha(mask); return image,这会导致图片仅在蒙版的黑色区域中透明。

您可能需要先使用image.convert('RGBA')转换图像,但我认为在PIL的更高版本中这是不必要的,它会自动执行。

类似于:(抱歉现在无法测试)

def round_corner_jpg(image, radius):
    """generate round corner for image"""
    mask = Image.new('L', image.size) # filled with black by default
    draw = aggdraw.Draw(mask)
    brush = aggdraw.Brush('white')
    width, height = mask.size
    #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()
    image = image.convert('RGBA')
    image.putalpha(mask)
    return image