为什么我得到'IOError异常值:[Errno 2]没有这样的文件或目录'用PIL保存文件时

时间:2011-03-19 05:16:39

标签: python django python-imaging-library ioerror

我正在使用Django / Python构建网站,并且有一个页面,用户可以上传照片,然后使用以下功能重新调整大小并保存:

from PIL import Image, ImageFile

def resize(file_to_resize, string_id,):
    parser = ImageFile.Parser()
    while True:
        s = file_to_resize.read(1024)
        if not s:
            break
        parser.feed(s)
    image = parser.close()
    ms = image.copy()
    size = 320,240
    ms.thumbnail(size, Image.ANTIALIAS)
    ms.save('http://www.mysite.com/site_media/images/profile/' + string_id, "JPEG") `

这在本地运行时工作正常,但是一旦我将网站上传到我的主机并在Apache下运行它,我收到以下错误:

请求方法:POST
请求网址:http://www.mysite.com/saveimage/8/
Django版本:1.2.1
异常类型:IOError
例外价值:
[Errno 2]没有这样的文件或目录:u'http://www.mysite.com/site_media/images/profile/8'
例外位置:/usr/local/lib/python2.6/site-packages/PIL/Image.py in save,line 1299
Python可执行文件:/usr/languages/python/2.6/bin/python
Python版本:2.6.6

另外,我是网络开发和Django / Python的新手,这实际上是我正在研究的项目,并计划与朋友和家人分享,所以我不确定这是否是最好的方法当涉及到安全性并允许将文件直接写入站点时。任何建议都表示赞赏。

1 个答案:

答案 0 :(得分:3)

当前目录中没有名为“http:”的目录。

如果您实际尝试通过HTTP保存文件,则不是这样。