Discord 为什么这个PIL生成的图像会被奇怪地扭曲?
我正在尝试制作一个discord.py机器人,并发出一个命令,发送一个带有目标头像的图像,以及上面的拍手图像。然而,它只是发送了一张没有头像的奇怪的拍手照片。 我的代码: 实际用户的个人资料图片: 它将实际用户的个人资料图片保存为: 它的输出:Discord 为什么这个PIL生成的图像会被奇怪地扭曲?,discord,python-imaging-library,discord.py,Discord,Python Imaging Library,Discord.py,我正在尝试制作一个discord.py机器人,并发出一个命令,发送一个带有目标头像的图像,以及上面的拍手图像。然而,它只是发送了一张没有头像的奇怪的拍手照片。 我的代码: 实际用户的个人资料图片: 它将实际用户的个人资料图片保存为: 它的输出: 我这样做了slap命令。如果您想参考,它可以工作并提供适当的输出 @commands.command() async def slap(self,ctx, user : discord.Member =None): try:
我这样做了slap命令。如果您想参考,它可以工作并提供适当的输出
@commands.command()
async def slap(self,ctx, user : discord.Member =None):
try:
if not user:
user=ctx.author
await ctx.send("Just slapped :wave: "+user.mention)
response = requests.get(user.avatar_url)
image_bytes = io.BytesIO(response.content)
person = Image.open(image_bytes)
slap = Image.open(".\cogs\slapping\slap.jpg")
person_res=person.resize((100,100))
area=(100,100,200,200)
slap.paste(person_res, area)
slap.save(".\\cogs\\slapping\\"+str(user.id)+".jpg")
with open(".\\cogs\\slapping\\"+str(user.id)+".jpg", 'rb') as f:
picture = discord.File(f)
await ctx.send(file=picture)
os.remove(".\\cogs\\slapping\\"+str(user.id)+".jpg")
except Exception as e:
print(e)
它生成的输出是:
在cogs文件夹中需要一个slapping文件夹,您可以在其中存储名为“slap.jpg”的图片。如果需要修改,只需更改代码中的目录。如果在转换模式中使用
'RGBA'
会发生什么?例如,im=Image.open(文件名).convert(“RGBA”)
@commands.command()
async def slap(self,ctx, user : discord.Member =None):
try:
if not user:
user=ctx.author
await ctx.send("Just slapped :wave: "+user.mention)
response = requests.get(user.avatar_url)
image_bytes = io.BytesIO(response.content)
person = Image.open(image_bytes)
slap = Image.open(".\cogs\slapping\slap.jpg")
person_res=person.resize((100,100))
area=(100,100,200,200)
slap.paste(person_res, area)
slap.save(".\\cogs\\slapping\\"+str(user.id)+".jpg")
with open(".\\cogs\\slapping\\"+str(user.id)+".jpg", 'rb') as f:
picture = discord.File(f)
await ctx.send(file=picture)
os.remove(".\\cogs\\slapping\\"+str(user.id)+".jpg")
except Exception as e:
print(e)