Reputation: 11
I have a Django app that gathers data via a multipart form inclusive of both POST and FILES values. The form data is received correctly by my Django views, and I am trying to process the image files passed via the form so as to produce thumbnails via PIL.Image. However, when I call the Image.thumbnail() method (or, for that matter, any method other than Image.open()) I get an IOError which I am unable to investigate any further.
Here's the code:
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)
The script raises the IOError exception, but the print gives me only "I/O error(None): None". Please note that fout.write() successfully writes my file to the selected path, and that Image.open() as well as Image.getbands() work successfully (the latter rightly returns, ('R', 'G', 'B')). But anything that calls Image.load() - in this case Image.thumbnail() - does not seem to work.
Any ideas?
Upvotes: 1
Views: 344
Reputation: 29344
(Moving the answer from the question field to the answer field)
I modified the exception to print the IOError as text, and got 'decoder jpeg not available'. However, a
brew install libjpeg
revealed that the library had already been installed. For whatever reason, Pillow wasn't seeing it. So I uninstalled and reinstalled Pillow withpip uninstall pillow
andpip install pillow
.
Upvotes: 0