2

我正在使用 django。我试图使用 Pythons Image library 将用户上传的图标压缩到更小的尺寸。

以下是我的代码:

def resizeImage(icon,ext):
     path= os.path.join(settings.SITE_ROOT,'karnadash/static/tempfiles/temp'+ext)
     destination = open(path,'wb+')
     for chunk in icon.chunks():
         destination.write(chunk)
     destination.close()
     image = Image.open(path)
     image= image.resize((50, 50), Image.ANTIALIAS)
     image.save(path)
     return image

问题是我收到内部服务器错误。堆栈跟踪的最后一部分如下:

 line 31, in resizeImage
     image.save(path)
 File "C:\Python27\lib\site-packages\PIL\Image.py", line 1446, in save
     fp = builtins.open(fp, "wb+")
IOError: [Errno 22] invalid mode ('wb') or filename: 'C:/Users/Silent/Documents/Python/karnadash/karnadash/static/tempfiles/temp.jpg'

有人可以解释为什么会这样吗?

4

3 回答 3

3

为我解决的问题是从反斜杠切换到正斜杠!谁会想到?!

类似帖子:ioerror 无效模式 w

于 2015-08-27T01:33:07.250 回答
0

检查您的文件路径是否有效:

C:/Users/Silent/Documents/Python/karnadash/karnadash/static/tempfiles/temp.jpg

也许它包含karnadash太多。

于 2013-07-04T11:19:04.540 回答
-1

当我试图将一些数字保存在饲料中时,我遇到了类似的问题。我可以保存一些数字,但无法保存其他数字,而且我使用的是相同的代码。我意识到图形的名称和反斜杠与保留代码冲突。

IOError: [Errno 22] invalid mode ('wb') or filename: '02102016\nDTG.png'

我认为"\n"被解释为“进入”。当我将其更改为正斜杠时,问题已解决。

于 2016-10-06T12:23:31.653 回答