PIL Image.thumbnail()因未知IOError而失败

时间:2017-03-02 13:43:06

标签: django image python-2.7 thumbnails pillow

我有一个Django应用程序,它通过包含POST和FILES值的多部分表单收集数据。我的Django视图正确接收表单数据,我正在尝试处理通过表单传递的图像文件,以便通过PIL.Image生成缩略图。但是,当我调用Image.thumbnail()方法(或者,就此而言,除Image.open()以外的任何方法)时,我得到一个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异常,但是print只给了我" I / O错误(无):无"。请注意fout.write()成功将我的文件写入选定的路径,Image.open()以及Image.getbands()成功运行(后者正确返回,(' R', ' G',' B'))。但是任何调用Image.load()的东西 - 在这种情况下是Image.thumbnail() - 似乎都不起作用。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

(将答案从问题字段移至答案字段)

  

我修改了异常以将IOError打印为文本,并且解码器jpeg不可用'。但是, yourArray = [jsonDict objectForKey:@"ClassTimings"]; NSString *stgmt = [yourArray objectAtIndex:indexPath.row]objectForKey:@"GMT"];; NSString *strtime = [yourArray objectAtIndex:indexPath.row]objectForKey:@"Time"];; NSString *strtimeZone = [yourArray objectAtIndex:indexPath.row]objectForKey:@"TimeZone"];; 显示已经安装了库。无论出于何种原因,Pillow都没有看到它。所以我使用brew install libjpegpip uninstall pillow卸载并重新安装了枕头。