【问题标题】:Why am I getting 'IOError Exception Value: [Errno 2] No such file or directory' When saving file with PIL为什么使用 PIL 保存文件时出现“IOError Exception Value: [Errno 2] No such file or directory”
【发布时间】:2011-07-18 15:09:50
【问题描述】:

我正在使用 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 保存,第 1299 行
Python 可执行文件:/usr/languages/python/2.6/bin/python
Python 版本:2.6.6

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

【问题讨论】:

    标签: python django python-imaging-library ioerror


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢,你说得对,我把这行改成:ms.save('site_media/images/profile/' + string_id, "JPEG") 就成功了。
    猜你喜欢
    • 1970-01-01
    • 2013-07-23
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-13
    • 2011-01-24
    相关资源
    最近更新 更多