Python Pillow为圆形图像添加边框

时间:2017-02-06 11:28:40

标签: python pillow

我一直在寻找这个,但在任何地方都找不到答案。我想要做的是为圆形图像添加白色边框。您可能知道,使用Pillow屏蔽图像时,会出现一种奇怪的边框,这就是为什么我要改变它。我使用以下代码来舍入原始图像,但不知道如何为此添加边框,因为它只会为原始图像创建一个矩形边框

 def add_corners(self, 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

希望有人知道如何做到这一点,感谢任何帮助!

0 个答案:

没有答案