大佬教程收集整理的这篇文章主要介绍了为什么这个 PIL 图像没有越过另一个?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试制作一个 discord.py 机器人,并制作一个命令,发送带有目标头像和拍打手图像的图像。然而,它只是发送他们的个人资料图片,没有别的。
我的代码:
@commands.command()
async def avatar(self,ctx,user : discord.Member):
"""User Avatar TesTing"""
filename = f"{user.namE}#{user.discriminator}.png"
await user.avatar_url.save(fileName)
fileslap = Image.open("assets/slap.png","r")
im = Image.open(fileName)
resized_im = im.resize((128,128))
img_w,img_h = fileslap.size
bg_w,bg_h = resized_im.size
offset = ((bg_w - img_w) // 2,(bg_h - img_h) // 2)
resized_im.paste(fileslap,offset)
resized_im.save(f"resized_{filename}")
file = discord.file(fp=f"resized_{filename}")
await ctx.send(file=filE)
os.remove(f"resized_{filename}")
您似乎只保存和发送调整大小的图像。您可能希望使用 resized_im.paste
而不是 im.paste
。
我像这样制作了 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”的图片。如果需要修改,只需更改代码中的目录即可。
这里提出了一个与 discord bot 的 slap 命令相关的类似问题:Why is this PIL-Generated Image weirdly distorted?
,尝试在打开图像后使用 .convert("RGB"),例如 im = Image.open(Name).convert("RGB") 或者如果图像中有一些透明度,可以解释奇怪的东西,使用 alpha_composite() 而不是 paste()。尝试,因为没有图像,无法真正理解。
以上是大佬教程为你收集整理的为什么这个 PIL 图像没有越过另一个?全部内容,希望文章能够帮你解决为什么这个 PIL 图像没有越过另一个?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。