如何将base64解码为image discord.py并发送



所以我找到了这个API,它有助于图像处理。它以base64格式给出结果。我搜索了很多,但无法找到如何准确地将base64解码为图像。代码如下:-

@bot.command()
async def image(ctx, method=None):
params = {'method': method, 'img1': 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png'}
header = {"Authorization": "mykey"}
async with aiohttp.ClientSession(headers=header) as session:
async with session.post(f'https://api.pgamerx.com/v5/canvas', params=params) as resp:
res = await resp.json()
data = res[0]["base64"]
print(data)

我打印数据以获得base64字符串,它非常大,在粘贴它时几乎崩溃了我的电脑。如有任何帮助,不胜感激。

(注)代码没有问题,一切正常

import io, base64  # Add these imports to the top of the file
@bot.command()
async def image(ctx, method=None):
params = {'method': method, 'img1': 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png'}
header = {"Authorization": "mykey"}
async with aiohttp.ClientSession(headers=header) as session:
async with session.post(f'https://api.pgamerx.com/v5/canvas', params=params) as resp:
res = await resp.json()
data = res[0]["base64"]
print(data)
file = discord.File(io.BytesIO(base64.b64decode(data)))
await ctx.send(file=file)

使用base64。B64decode解码base64编码的字符串,然后将结果包装在字节组中,以便它可以被discord读取。文件

最新更新