我尝试将图像编辑为 gif。 它应该向用户头像添加一个图像,一个过滤器并移动图像。 我在发送编辑后的文件时遇到了麻烦。 (我不知道代码到目前为止是否有效)
class BadRequest(Exception):
def __init__(self, error):
super().__init__(error)
def get(url, **kwargs):
res = requests.get(url, **kwargs)
return res
def get_content_raw(url, **kwargs):
return get(url, stream=True, **kwargs).content
def get_image(url, **kwargs):
try:
raw = get_content_raw(url, **kwargs)
return Image.open(BytesIO(raw))
except OSError:
raise BadRequest('An invalid image was provided! Check the URL and try again.')
def editit(ctx, avatars):
global randint
avatar = get_image(avatars).resize((320, 320)).convert('RGBA')
image_s = get_image('https://example.com/image/image.bmp')
tint = get_image('https://example.com/image/filter.bmp').convert('RGBA')
blank = Image.new('RGBA', (256, 256), color=(231, 19, 29))
frames = []
for i in range(8):
base = blank.copy()
if i == 0:
base.paste(avatar, (-16, -16), avatar)
else:
base.paste(avatar, (-32 + randint(-16, 16), -32 + randint(-16, 16)), avatar)
base.paste(tint, (0, 0), tint)
if i == 0:
base.paste(image_s, (-10, 200))
else:
base.paste(image_s, (-12 + randint(-8, 8), 200 + randint(0, 12)))
frames.append(base)
b = BytesIO()
frames[0].save(b, save_all=True, append_images=frames[1:], format='gif', loop=0, duration=20, disposal=2,
optimize=True)
b.seek(0)
return ctx.send(file=b)
@bot.command()
async def example(ctx, avatars: discord.Member):
await ctx.message.delete()
avatars = avatars.avatar_url
await editit(ctx, avatars)
但输出是:
InvalidArgument: file parameter must be File
我是这种代码的新手,所以我需要一些帮助。 提前致谢
答案 0 :(得分:1)
您必须发送 discord.File
对象,而不是字节 io 对象。
return ctx.send(file = discord.File(b, "unknown.png"))
"unknown.png"
是图像的文件名。 (可选)
完整文档: https://discordpy.readthedocs.io/en/latest/api.html#file