我的discord机器人在某个命令后创建二维码。但是,我无法将此二维码作为消息发送给用户:
import qrcode
def create_qr_code(string : str):
qr = qrcode.make(string)
return qr
# sending qr to user
qr_code = create_qr_code('some text')
# check if qr_code is None
print(qr_code)
await ctx.send(file=discord.File(fp=qr_code))
我的print
语句返回如下内容
<qrcode.image.pil.PilImage object at 0x000001BD735FCF28>
,
这很好,并向我表明二维码的创建是成功的。我想知道为什么发送它似乎不起作用。
发布于 2021-11-14 11:49:58
实际上,我自己使用this solution找到了一个有效的解决方案:
首先,我创建了一个二维码并返回这个对象
import qrcode
def create_qr_code(string : str):
qr_code = qrcode.make(string)
return qr_code
我现在可以使用BytesIO()
将此二维码作为二进制文件发送给discord:
import io
def some_other_function():
qr_code = create_qr_code('my string')
with io.BytesIO() as image_binary:
qr_code.save(image_binary, 'PNG')
image_binary.seek(0)
await ctx.send(file=discord.File(fp=image_binary, filename='qr.png'))
发布于 2021-11-10 22:25:59
您可以使用一个名为qrcode的包,然后使用以下代码:
async def qrcode(self, ctx, *, url):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4,
)
qr.add_data(str(url))
qr.make(fit=True)
img = qr.make_image(fill_color="black",
back_color="white").convert('RGB')
img.save('qrcode.png')
await ctx.send(file=discord.File('qrcode.png'))
顺便说一句,如果你想继续使用PyQRCode,看看pypi文档,看起来你可以这样做:
qr_code.png('code.png', scale=6, module_color=[0, 0, 0, 128], background=[0xff, 0xff, 0xcc])
来拯救它。
https://stackoverflow.com/questions/69913223
复制相似问题