Python discord.py/Image:如何使用Image编辑ctx.author配置文件图片,并将其沙回到discord上?

2024-09-30 10:35:49 发布

您现在位置:Python中文网/ 问答频道 /正文

嘿,我尝试在discord上获取头像图片,然后在上面添加文本,最后将其发送回tchat

因此,我尝试了以下方法来获取用户的图像:

  • 步骤1:获取用户化身url
  • 步骤2:获取更改url以获得正确大小的图片
  • 步骤3:请求图像
  • 步骤4:使用image.open打开图像

这是我执行以下步骤的代码:

@bot.command(name="pic")
async def get_pic(ctx):
  url = f"{str(ctx.author.avatar_url)[:-4]}128" # get url and change 1024 to 128
  avatar = Image.open(urllib.request.urlopen(url))

不幸的是,我得到一个错误403:禁止。 如何解决这个问题


Tags: 方法用户图像文本urlget步骤open
2条回答

那么它是这样描述的

The HTTP response code 403 mean access to the requested resource is forbidden. The server understood the request, but will not fulfill it.

意思是服务器收到了你的请求,但我猜你没有权限

快乐编码

您可以使用^{}获取表示所需大小的化身的^{} object,将该数据读入类似文件的对象,然后使用PIL打开该类似文件的对象

from PIL import Image
from io import BytesIO

@bot.command(name="pic")
async def get_pic(ctx):
    asset = ctx.author.avatar_url_as(size=128)
    data = BytesIO(await asset.read())
    image = Image.open(data)

相关问题 更多 >

    热门问题