Python, QRCODE -如何显示图像



我成功地生成了一个代码,我认为,使用qrcode库。当我运行该命令时,它不会抛出任何错误。我现在如何将文件保存为.png ?

这是我到目前为止的代码:

import qrcode

qr = qrcode.QRCode(version=1,
        error_correction=qrcode.constants.ERROR_CORRECT_L,
        box_size=10,
        border=4,
        )
qr.add_data("This is a test string")
qr.make(fit=True)
img = qr.make_image()

您需要显式地将图像数据保存到文件中,如下所示:

with open('myfile.png', 'wb') as f:
    img.save(f)

编辑:显然qrcode需要安装这些包来保存图像:

pip install git+git://github.com/ojii/pymaging.git#egg=pymaging
pip install git+git://github.com/ojii/pymaging-png.git#egg=pymaging-png

最新更新