2017-03-02 18 views
1

我有一个Django应用程序通过包含POST和FILES值的多部分表单收集数据。我的Django视图正确接收表单数据,并且我正在尝试处理通过表单传递的图像文件,以便通过PIL.Image生成缩略图。但是,当我调用Image.thumbnail()方法(或者,除此之外,Image.open())以外的任何方法)时,我得到一个IOError,我无法进一步调查。PIL Image.thumbnail()失败,发生未知IOError

下面的代码:

from PIL import Image 
import io 
import os 
from django.core.files.uploadedfile import SimpleUploadedFile 
from django.core.files.base import ContentFile 
from django.conf import settings 

def generate_thumbnail(file): 
if 'image' in file.content_type: 
    print "about to save the thumbnail" 
    file_content= ContentFile(file.read()) 
    print "file read" 
    full_filename = os.path.join(settings.MEDIA_ROOT, file.name) 
    print "path selected" 
    fout = open(full_filename, 'wb+') 
    for chunk in file_content.chunks(): 
     fout.write(chunk) 
    fout.close() 
    print " file saved" 
    im = Image.open(open(full_filename, 'rb')) 
    print im.getbands() 
    #no problem up to this point 
    try: 
     size = 128,128 
     thumb = im.thumbnail(size) 
    except IOError as e: 
     print "I/O error({0}): {1}".format(e.errno, e.strerror) 

脚本引发IOError异常,但打印给我唯一的 “I/O错误(无):无”。请注意,fout.write()成功地将我的文件写入选定的路径,并且Image.open()以及Image.getbands()成功工作(后者正确地返回,('R','G',' B'))。但是任何调用Image.load()的东西 - 在这种情况下Image.thumbnail() - 似乎都不起作用。

任何想法?

回答

0

(移动从问题领域的答案回答域)

我修改了异常打印的IOError为文本,并得到了“解码JPEG不可用”。但是,brew install libjpeg显示该库已安装。无论出于何种原因,枕头都没有看到它。所以我卸载并重新安装了枕头pip uninstall pillowpip install pillow

相关问题