2017-03-02 19 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

相關問題