我正在使用django。我试图使用Pythons Image库将用户上传的图标压缩到较小的尺寸。
以下是我的代码:
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'
有人可以解释为什么会这样吗?
答案 0 :(得分:3)
为我解决的是从反斜杠切换到正斜杠!谁会想到的?!
答案 1 :(得分:0)
检查文件路径是否有效:
C:/Users/Silent/Documents/Python/karnadash/karnadash/static/tempfiles/temp.jpg
也许它包含一个karnadash
太多了。
答案 2 :(得分:-1)
IOError: [Errno 22] invalid mode ('wb') or filename: '02102016\nDTG.png'
我认为"\n"
被解释为“输入”。当我将其更改为正斜杠时,问题已得到解决。