尝试调用save函数会导致TypeError。作为旁注:qrcode需要将PIL导入为PilImage。
还值得一提的是,在使用Python Imaging Library时我没有遇到此错误。它只发生在(通过推荐)切换到Pillow之后。
from qrcode import *
import PIL as PilImage
from PIL import Image
import qrcode
qr = qrcode.QRCode (
version = 1,
error_correction = qrcode.constants.ERROR_CORRECT_L,
box_size = 10,
border = 2
)
qr.add_data('Hello World')
qr.make(fit=True)
img = qr.make_image()
img.format = 'PNG'
img.save('test.png')
Traceback (most recent call last):
File "<pyshell#70>", line 1, in <module>
img.save('test.png')
File "build\bdist.win32\egg\qrcode\image\pil.py", line 29, in save
self._img.save(stream, kind)
File "C:\Python27\lib\site-packages\PIL\Image.py", line 1467, in save
save_handler(self, fp, filename)
File "C:\Python27\lib\site-packages\PIL\PngImagePlugin.py", line 605, in _save
ImageFile._save(im, _idat(fp, chunk), [("zip", (0,0)+im.size, 0, rawmode)])
File "C:\Python27\lib\site-packages\PIL\ImageFile.py", line 452, in _save
e = Image._getencoder(im.mode, e, a, im.encoderconfig)
File "C:\Python27\lib\site-packages\PIL\Image.py", line 395, in _getencoder
return encoder(mode, *args + extra)
TypeError: function takes at most 4 arguments (6 given)
答案 0 :(得分:1)
作为最后一针,我重新安装了枕头模块。这奇迹般地解决了我的问题,img.save()
现在完全符合预期。我不确定如何,但在初始安装过程中必须出错。谢谢你的帮助。这种经历一直很糟糕。如果你想知道这个冲突在追求的是我的git-repository:https://github.com/NamesJ/qr-tickets
答案 1 :(得分:0)
img.size()
是否会返回长度为4的tupe?
在这种情况下,你应该让它返回一个长度为2的元组,如下所示:
size = img.size
img.size = size[0], size[1]
img.save('test.png')
试试并报告。可能还有其他可能性。