Solution
- Open the pfp image as numpy array, convert to RGB
img = Image.open(data).convert("RGB")
npImage = np.array(img)
h,w = img.size
- Create same size alpha layer with circle
alpha = Image.new('L', img.size,0)
draw = ImageDraw.Draw(alpha)
draw.pieslice([0,0,h,w],0,360,fill=255)
- Convert alpha Image to numpy array
npAlpha=np.array(alpha)
npImage=np.dstack((npImage,npAlpha))
- Save the image with alpha!
Image.fromarray(npImage).save('result.png')
Example
import numpy as np
from PIL import Image, ImageDraw
@client.event
async def on_member_join(member : discord.Member):
welcome = Image.open('Welcome.jpg')
asset = member.avatar_url_as(size = 128)
data = BytesIO(await asset.read())
img=Image.open(data).convert("RGB")
npImage=np.array(img)
h,w=img.size
alpha = Image.new('L', img.size,0)
draw = ImageDraw.Draw(alpha)
draw.pieslice([0,0,h,w],0,360,fill=255)
npAlpha=np.array(alpha)
npImage=np.dstack((npImage,npAlpha))
pfp = Image.fromarray(npImage)
welcome.paste(pfp, (657, 257))
welcome.save("profile.jpg")