如何将图像裁剪为例如形状.在 Discord.py 中使用 Pillow 圈?
问题描述
如何将图像裁剪为例如形状.在 Discord.py 中使用 Pillow 循环?
How can I crop an image to a shape for eg. circle using Pillow in Discord.py?
我当前的代码:
@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())
pfp = Image.open(data)
pfp = pfp.resize((500, 500))
welcome.paste(pfp, (657, 257))
welcome.save("profile.jpg")
解决方案
试试这个
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")
相关文章