how to round_corner a logo without leaving white background(transparent?) with it using pil?
- by bdictator
I got a square logo and I need to round_corner it, searched for a while and got the follow code "working":
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(''))
#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)
then I saved the returned image object,however it has white background in it?how can i get rid of the white background?
Thanks in advance~