如何更改qrencode.encode的图片大小?

How to change the image size of qrencode.encode?

我正在使用 python 库 qrencode 创建 QR 码,其中 returns 一个 PIL 25x25 图像大小的元组。如何以更大的图像尺寸导出它?现在我正在导出为 PDF,缩放 1000% 并从那里手动...

import qrencode as qre

url = "http://some.url.com"
qrTuple = qre.encode(url)
qrPIL = qrTuple[2]

filename = 'filename.png'
qrPIL.save(filename)

您可以使用.resize()函数:

将此代码添加到程序末尾:

big = qrPIL.resize((1024, 1024))
big.save("bigger.png")
big.show()

qrencode 库正在使用 PIL 而 returns 您只是一个 PIL 图像,因此您可以使用 PIL Image 方法调整大小以在保存之前缩放图像。这里我使用最近邻过滤器调整大小,因为它不会尝试对 QR 码的像素进行插值,所以它不会扭曲它并会保留清晰的线条。

from PIL import Image

qrPIL = qrPIL.resize((256, 256), Image.NEAREST) # 256 pixels x 256 pixels
qrPIL.save('resized.png')

这是 PIL Image methods 的完整列表,您可以在创建 QR 码后使用它来转换它。