Python,QRCODE - 如何显示图像?

时间:2015-10-14 12:12:58

标签: python image

我认为,我使用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()

1 个答案:

答案 0 :(得分:3)

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

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
相关问题