首页 > python-3.x > 如何将qr_code img导出到磁盘?

如何将qr_code img导出到磁盘? (How to export qr_code img to disk?)

问题

我使用python包qrcode从url生成一个qr代码图像:

url='www.google.com'
qr = qrcode.QRCode(version=1,
                   error_correction=qrcode.constants.ERROR_CORRECT_L,
                   box_size=10,
                   border=4)

qr.add_data(url)
qr.make(fit=True)
img = qr.make_image()
return img

如何在特定路径上将img保存到磁盘?

我试过了:

image_file = open(path_qr_code, 'w')
image_file.write(img)
image_file.close

但我得到:

TypeError:write()参数必须是str,而不是PilImage

解决方法

试试这个:

img.save("img.png","PNG")

或路径:

img.save(path_qr_code + "\\" + "imgtest.png","PNG")

问题

I generate a qr code image from an url using the python package qrcode:

url='www.google.com'
qr = qrcode.QRCode(version=1,
                   error_correction=qrcode.constants.ERROR_CORRECT_L,
                   box_size=10,
                   border=4)

qr.add_data(url)
qr.make(fit=True)
img = qr.make_image()
return img

How do I save the img to the disk at a certain path ?

I tried:

image_file = open(path_qr_code, 'w')
image_file.write(img)
image_file.close

but I get:

TypeError: write() argument must be str, not PilImage

解决方法

Try this:

img.save("img.png","PNG")

or with path:

img.save(path_qr_code + "\\" + "imgtest.png","PNG")
相似信息