You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在discord.py的Embed中添加easy_pil生成的图片?

在discord.py中嵌入easy_pil生成的图片到Embed

直接按以下步骤操作即可,结合你已有的代码,完整实现如下:

完整代码示例

from discord import Embed, File
from easy_pil import Editor

# 你的图片生成代码
background = Editor('./assets/card_1.jpg').resize((900, 300))
background.rectangle((30,220), width=600, height=30, fill='#C0B6CC', radius=20)
background.bar((30,220), max_width=600, height=30, percentage=90, fill='#7600FF', radius=20)
file = File(fp=background.image_bytes, filename='card.png')

# 创建Embed并设置图片
embed = Embed(title="自定义进度卡片", description="使用easy_pil生成的可视化进度条")
# 关键:用attachment://文件名的格式指向本地附件
embed.set_image(url="attachment://card.png")

# 发送消息(以命令上下文为例,交互命令同理)
# 如果是前缀命令:
await ctx.send(embed=embed, file=file)
# 如果是斜杠命令:
# await interaction.response.send_message(embed=embed, file=file)

核心注意点

  • 必须将生成的file对象作为参数传递给send()方法,否则Discord无法加载这个附件
  • set_image的url参数必须使用attachment://[文件名]的格式,且文件名要和你创建File时指定的filename完全一致
  • 如果需要多次发送不同的生成图片,确保每次的filename唯一,避免出现加载异常

内容的提问来源于stack exchange,提问作者FlamyIce

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 23:55:18