【问题标题】:Discord.py Image EditingDiscord.py 图像编辑
【发布时间】:2022-04-18 13:30:51
【问题描述】:

我注意到一些机器人(例如 MEE6、Arcane、Tatsu 等等)可以获取用户的个人资料并将其添加到另一个图像上。有没有办法在 Discord.py 中做到这一点? (对不起,如果我错过了在线或其他东西。)

【问题讨论】:

  • 我只是浏览了他们的 API 参考。看起来他们没有内置任何可以处理图像编辑的东西。我认为他们很可能使用不同的 Python 包来创建图像,可能类似于 Pillow:pillow.readthedocs.io/en/stable

标签: discord.py


【解决方案1】:

这被称为image manipulation,它可以在 Discord.py 中使用pillow 完成,它会获取图像并保存它与另一个称为“操作”的组合

这些是枕头所需的主要进口:

from PIL import Image
from io import BytesIO
from PIL import ImageFont
from PIL import ImageDraw
from PIL import ImageOps

在图片上生成用户个人资料图像的简单命令可以在下面完成,确保放入图片 url 或文件,以便它可以覆盖它。

@client.command()
async def test(ctx, user: discord.Member = None):

        my_image = Image.open("Put your image link or file here")

        asset = user.avatar_url_as(size=128)
        data = BytesIO(await asset.read())
        pfp = Image.open(data)

        pfp = pfp.resize((125, 125))
        my_image.paste(pfp, (36, 80))

        my_image.save("profile.png")

        await ctx.send(file=discord.File("profile.png"))

在此示例中,机器人首先检索图像内容,然后将其存储。它将asset 作为用户的头像 url 并将其读取为字节,但这太深了,无法进一步解释。然后它会拍摄两张图像,然后定位它,保存文件并发送它。

您可以在他们的指南中进一步找到有关枕头的更多信息:https://pillow.readthedocs.io/en/stable/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-15
    • 2021-05-13
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多