Error: "Cannot identify image file" PIL + Django

1,158 views
Skip to first unread message

Jacolyte

unread,
May 10, 2009, 7:32:19 PM5/10/09
to Django users
I have a model that has an ImageField, and when I try to work with
this model in the admin interface by uploading an image of any type, I
usually receive the error "Cannot identify image file" - but there is
only one exception to this, which is when I use a picture taken from
my digital camera. *nothing* else will go through, I've
tried .gifs, .pngs, and .jpgs

I've transferred my Django project to my production machine, and it
works beautifully and accepts all images. They both run the most
recent SVN release on Python 2.6 using PIL 1.1.6

I received this friendly message when PIL was built:
--- TKINTER support ok
--- JPEG support ok
--- ZLIB (PNG/ZIP) support ok
--- FREETYPE2 support ok

Then the PIL tests:
blaine@blaine-laptop ~/apps/Imaging-1.1.6 $ python selftest.py
57 tests passed.

Seems to work on the python interactive shell:
>>> from PIL import Image
>>> from cStringIO import StringIO
>>> img = Image.open(StringIO("/home/blaine/Pictures/Me/neck-beard.jpg").read())
>>> img
<PIL.JpegImagePlugin.JpegImageFile instance at 0xb770948c>


But then I receive the following traceback when trying to upload an
image of any type (.jpg, .png, or .gif):
===================================================
Environment:

Request Method: POST
Request URL: http://localhost:8000/admin/music/album/add/
Django Version: 1.1 beta 1 SVN-10731
Python Version: 2.6.2
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'jacolyte.music',
'sorl.thumbnail',
'tagging']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware')

Traceback:
File "/home/blaine/site-packages/django/core/handlers/base.py" in
get_response
92. response = callback(request, *callback_args,
**callback_kwargs)
File "/home/blaine/site-packages/django/contrib/admin/options.py" in
wrapper
226. return self.admin_site.admin_view(view)(*args,
**kwargs)
File "/home/blaine/site-packages/django/contrib/admin/sites.py" in
inner
184. return view(request, *args, **kwargs)
File "/home/blaine/site-packages/django/db/transaction.py" in
_commit_on_success
240. res = func(*args, **kw)
File "/home/blaine/site-packages/django/contrib/admin/options.py" in
add_view
715. if form.is_valid():
File "/home/blaine/site-packages/django/forms/forms.py" in is_valid
120. return self.is_bound and not bool(self.errors)
File "/home/blaine/site-packages/django/forms/forms.py" in _get_errors
111. self.full_clean()
File "/home/blaine/site-packages/django/forms/forms.py" in full_clean
238. value = field.clean(value, initial)
File "/home/blaine/site-packages/django/forms/fields.py" in clean
511. trial_image = Image.open(file)

Exception Type: IOError at /admin/music/album/add/
Exception Value: cannot identify image file
===================================================


Some context as to where the error occured (line 511):
===================================================
/home/blaine/site-packages/django/forms/fields.py in clean:
504. file = StringIO(data.read())
505. else:
506. file = StringIO(data['content'])
507.
508. try:
509. # load() is the only method that can spot a truncated JPEG,
510. # but it cannot be called sanely after verify()

511. trial_image = Image.open(file) ...

512. trial_image.load()
513.
514. # Since we're about to use the file again we have to reset the
515. # file object if possible.
516. if hasattr(file, 'reset'):
517. file.reset()

▼ Local vars
Image <module 'PIL.Image' from '/usr/lib/python2.6/dist-packages/PIL/
Image.pyc'>
data <InMemoryUploadedFile: crystal.png (image/png)>
f <InMemoryUploadedFile: crystal.png (image/png)>
file <cStringIO.StringI object at 0x96ae0b0>
initial None
self <django.forms.fields.ImageField object at 0x95a804c>
===================================================

System information:
Django Version: 1.1 beta 1 SVN-10731
Python Version: 2.6.2
PIL Version: 1.1.6
Server: Django dev server
OS: Ubuntu 9.04

I tried installing PIL from the ubuntu repositories and from source,
both cases give me the exact same error message.

Any help on this would be greatly appreciated, thank you!

Karen Tracey

unread,
May 10, 2009, 8:07:33 PM5/10/09
to django...@googlegroups.com
2009/5/10 Jacolyte <brlafr...@gmail.com>

I have a model that has an ImageField, and when I try to work with
this model in the admin interface by uploading an image of any type, I
usually receive the error "Cannot identify image file" - but there is
only one exception to this, which is when I use a picture taken from
my digital camera. *nothing* else will go through, I've
tried .gifs, .pngs, and .jpgs

I've transferred my Django project to my production machine, and it
works beautifully and accepts all images. They both run the most
recent SVN release on Python 2.6 using PIL 1.1.6

[snip debug details]

System information:
Django Version: 1.1 beta 1 SVN-10731
Python Version: 2.6.2
PIL Version: 1.1.6
Server: Django dev server
OS: Ubuntu 9.04

I tried installing PIL from the ubuntu repositories and from source,
both cases give me the exact same error message.

Any help on this would be greatly appreciated, thank you!

A bug was introduced in a very recent file storage change that affects file uploads.  You might want to try the patch on this ticket:

http://code.djangoproject.com/ticket/11055

The message you are getting is slightly different, and I don't understand how, if you are running r10717 or higher on your production machine as well, you don't see the problem there (nor why you don't see it with pictures from your camera), but it is a fix you will need to get file uploads working properly on the level of Django you are running.

Karen

Jacolyte

unread,
May 10, 2009, 9:05:35 PM5/10/09
to Django users
On May 10, 6:07 pm, Karen Tracey <kmtra...@gmail.com> wrote:
> 2009/5/10 Jacolyte <brlafreni...@gmail.com>
>
>
>
>
>
> > I have a model that has an ImageField, and when I try to work with
> > this model in the admin interface by uploading an image of any type, I
> > usually receive the error "Cannot identify image file" - but there is
> > only one exception to this, which is when I use a picture taken from
> > my digital camera. *nothing* else will go through, I've
> > tried .gifs, .pngs, and .jpgs
>
> > I've transferred my Django project to my production machine, and it
> > works beautifully and accepts all images. They both run the most
> > recent SVN release on Python 2.6 using PIL 1.1.6
>
> > [snip debug details]
> > System information:
> > Django Version: 1.1 beta 1 SVN-10731
> > Python Version: 2.6.2
> > PIL Version: 1.1.6
> > Server: Django dev server
> > OS: Ubuntu 9.04
>
> > I tried installing PIL from the ubuntu repositories and from source,
> > both cases give me the exact same error message.
>
> > Any help on this would be greatly appreciated, thank you!
>
> The message you are getting is slightly different, and I don't understand
> how
I altered the Exception handling in fields.py to just raise whatever
exception came along, rather than throw a validation error. I got the
idea here:
http://jgeewax.wordpress.com/2008/07/28/debugging-django-image-uploading/
> it is a fix you will need to get file uploads working
> properly on the level of Django you are running.
I applied the patch, and it worked wonderfully.

Thank you.
Reply all
Reply to author
Forward
0 new messages