Skip to main content
 首页 » 编程设计

python之尝试导出 JPEG 时出现 TypeError

2025年05月04日10yjmyzz

我正在尝试使用以下代码导出给定图像的调整大小的 JPEG(下载部分被省略,因为它工作正常):

basewidth = 400 # user-defined variable 
wpercent = (basewidth/float(img.size[0])) 
hsize = int((float(img.size[1])*float(wpercent))) 
img = img.resize((basewidth,hsize), Image.ANTIALIAS) 
theitemishere = "/home/myusername/public_html/resizer/" + filename 
img.save(theitemishere + extension, extension_caps) 

但是,在保存新图像时出现以下错误(这是回溯):

  File "/home/myusername/public_html/cgi-bin/PIL/Image.py", line 1467, in save 
    save_handler(self, fp, filename) 
  File "/home/myusername/public_html/cgi-bin/PIL/JpegImagePlugin.py", line 557, in _save 
    ImageFile._save(im, fp, [("jpeg", (0,0)+im.size, 0, rawmode)]) 
  File "/home/myusername/public_html/cgi-bin/PIL/ImageFile.py", line 466, in _save 
    e = Image._getencoder(im.mode, e, a, im.encoderconfig) 
  File "/home/myusername/public_html/cgi-bin/PIL/Image.py", line 395, in _getencoder 
    return encoder(mode, *args + extra) 
TypeError: function takes at most 11 arguments (13 given) 

关于为什么会发生这种情况有什么想法吗?

FWIW,我无法在服务器上安装 PIL 模块,这就是为什么我将它作为 cgi-bin 的子目录。

请您参考如下方法:

我遇到了同样的问题并解决了。写这个解决方案是因为我觉得上面的答案对于其他遇到同样问题并正在寻找解决方案的人来说描述性不够

我遇到这个问题是因为我同时安装了 PIL n Pillow。所以不得不卸载其中之一。这解决了问题。

谢谢。