Python枕头为圆形图像添加边框

2024-04-19 23:18:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在找这个,但在任何地方都没有找到答案。我要做的是,给一个圆形的图像添加一个白色的边框。你可能知道,当用枕头遮住图像时,有一种奇怪的边界,这就是为什么我想改变它。我使用下面的代码来取整原始图像,但是还不知道如何添加边框,因为它只会为原始图像创建一个矩形边框

 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

希望有人知道怎么做,任何帮助,谢谢!在


Tags: 答案图像cropimagealphanewsize地方