PIL round edges and add border

孤人 提交于 2020-03-01 23:55:22

问题


I'm currently using this method to round the edges on images for my users:

def _add_corners(self, im, rad=100):
    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

The rounding works perfectly fine and i'm happy with it. However, I would like to also draw a border around the image within the constraints of the edges. Most of what I'm reading online shows how to draw border on the image itself (not the rounded border i'm doing). Is there a way to do that? I have read the below:

how to round_corner a logo without white background(transparent?) on it using pil?

Python Imaging Library (PIL) Drawing--Rounded rectangle with gradient

Any way to make nice antialiased round corners for images in python?

来源:https://stackoverflow.com/questions/38539277/pil-round-edges-and-add-border

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!