Skip to content

The source file does not appear to be an image #169

Closed
FunkyLoveCow opened this Issue Sep 21, 2012 · 20 comments

10 participants

@FunkyLoveCow

I have a model that looks like this:

from easy_thumbnails.fields import ThumbnailerImageField

class Photo(models.Model):
    photo = ThumbnailerImageField('Photo', upload_to="images/", null=False, blank=False)    

In a test view I am doing this:
from proj1_PoC.models import Photo

def test_view(request):
    pic = Photo.objects.get(pk = 1)
print pic.photo['thumbnail'].url
    return HttpResponse("End test_view")

My 'thumbnail' is defined thusly:
THUMBNAIL_ALIASES = {
'': {
'thumbnail': {'size': (200, 200), 'crop': True},
},
}

It errors on the print statement in the test_view:
Exception Type: InvalidImageFormatError
Exception Value: The source file does not appear to be an image
Exception Location: H:\dev\proj1_PoC\easy_thumbnails\files.py in generate_thumbnail, line 292

The uploaded image is definitely a .jpeg file. It was loaded via the admin control panel. There were no errors saving the image.

I saw #167 and merged that change, but doesn't appear related to this issue since I am not using 'resize_source'

@tomscytale

have you set THUMBNAIL_DEBUG to TRUE? sometimes it helps.

I had this problem. (THUMBNAIL_DEBUG didn't help). after some digging it turned out that exceptions from PIL were being hidden by a catch-all except statement. in my case the problem was that PIL hadn't found the system libraries libz and libjpeg when it was installed. the solution: http://stackoverflow.com/questions/8401085/error-in-pil-installed-with-pip-django-python/13771479#13771479
if this doesn't fix your problem then try editing the easy-thumbnails source to remove the catch-all except statements - that should cause any exceptions to become obvious.

@hemanth7787

I see this is 6 months old. But i have written this to help anybody passing by.

When using "virtualenv --no-site-packages" the PIL library in your python installation is not copied to virtual environment.

So when you "PIP install PIL" . PIL is compiled from source , and if you dont have libjpeg development libraries you will end up with PIL that dont have jpeg support (see below log)

PIL 1.1.7 SETUP SUMMARY
--------------------------------------------------------------------
version       1.1.7
platform      linux2 2.7.3 (default, Sep 26 2012, 21:51:14)
              [GCC 4.7.2]
--------------------------------------------------------------------
*** TKINTER support not available
*** JPEG support not available
*** ZLIB (PNG/ZIP) support not available
*** FREETYPE2 support not available
*** LITTLECMS support not available

And when you use easy_thumbnails it will show the same error as below:
"The source file does not appear to be an image"

A code to test PIL

after activating virtualenv(only if you use it), execute below script

from PIL import Image
import glob, os

size = 128, 128

for infile in glob.glob("*.jpg"):
file, ext = os.path.splitext(infile)
im = Image.open(infile)
im.thumbnail(size, Image.ANTIALIAS)
im.save(file + ".thumbnail", "JPEG")


this should create thumbnails for all *.jpg files in same directory.
if it doesn't PIL is the culprit.

you can put the above script in a file 'create_thumb.py" and execute

" python -m pdb create_thumb.py"

from PIL import Image
(Pdb) continue[enter]

@SmileyChris
Owner

Sorry for leaving this stagnant. This is most certainly a PIL configuration issue. I'd recommend using pillow instead, it builds nicer in a virtualenv and is actually getting updates as opposed to PIL.

@foxbunny

Another note to anyone on Windows. Do not install Pillow using pip. Instead, use easy_install. The reason is that pip doesn't install binary eggs even when available. and building Pillow from sources on Windows doesn't work. Using easy_install, a binary version is installed, which works like a charm.

@SmileyChris
Owner

Thanks for the note, @foxbunny.

Maybe you could volunteer to do python-pillow/Pillow#310 ? :)

@foxbunny

@SmileyChris Maybe. :)

@aclark4life

Please do!

@surfer26th surfer26th referenced this issue in ridethepony/django-fiber Dec 7, 2013
Closed

InvalidImageFormatError #184

@bhaugen
bhaugen commented Jan 19, 2014

I'm getting this same error on a jpeg that easy_thumbnails does find (see below) and I am using Pillow in a virtual env.
Ubuntu 10.04, Django 1.4.5.

http://dpaste.com/1560139/

.virtualenvs/vn/lib/python2.6/site-packages/easy_thumbnails/files.py in generate_thumbnail
if high_resolution:
orig_size = thumbnail_options['size'] # remember original size
thumbnail_options = thumbnail_options.copy()
thumbnail_options['size'] = (orig_size[0] * 2, orig_size[1] * 2)
image = self.generate_source_image(thumbnail_options)
if image is None:
raise exceptions.InvalidImageFormatError(
"The source file does not appear to be an image") ...
thumbnail_image = engine.process_image(image, thumbnail_options,
self.thumbnail_processors)
quality = thumbnail_options.get('quality', self.thumbnail_quality)
if high_resolution:
▼ Local vars
Variable Value
image

None
thumbnail_options

{'size': (128, 128)}
high_resolution
False
self

@mgaitan
mgaitan commented Feb 19, 2014

@bhaugen did u find a solution? i have the very same problem

@bhaugen
bhaugen commented Feb 19, 2014

No. Works for me as long as I use PIL, but not with PILLOW.

@mgaitan mgaitan referenced this issue in mgaitan/preciosa Feb 19, 2014
Closed

No funcionan los thumbnails en los listados #68

@thanhleviet

i had the same problem and after following foxbunny's note, it works perfectly

@SmileyChris
Owner

@ninomax If you're using a recent version of pip (try pip install --upgrade pip), it should pull in the binary wheel for windows systems now.

@atkinson

Also worth noting that this exception is thrown if the file does not exist, which can be slightly misleading.

@bhaugen
bhaugen commented Mar 25, 2014

Just set up a new machine, Ubuntu 12.04, newly installed everything. Pip installed Pillow without errors. Still getting the error above, and also "decoder jpeg not available" when trying to create a ThumbnailImageField from a jpeg.

Looks like I gotta revert to PIL again...

@aclark4life

@bhaugen What exactly is the issue & how does using PIL (instead of Pillow) resolve it? If you can describe these two things, please open a ticket here: https://github.com/python-imaging/Pillow/issues

@bhaugen
bhaugen commented Mar 25, 2014

@aclark4life Two issues:

  1. InvalidImageFormatError("The source file does not appear to be an image") when trying to render a ThumbnailImageField that is connected to a jpeg file.
  2. "decoder jpeg not available" when trying to create a ThumbnailImageField from a jpeg.

The history of this is:
1. I started using PIL, which worked fine.Then heard that Pillow is better and easier to install. (PIL is a pain.) So tried it in a new virtualenv with no site packages. Failed with the first error.

  1. Now I am setting up a new computer, with Ubuntu 12.04, no PIL installed at all (so far). I get both of those errors.

(Although I agree, I should open an issue on Pillow...sorry to complain about this one being closed.)

@SmileyChris
Owner

When pillow compiles, it shows a report of what decoders are available. What does it show there?

Jump on #django if you like and we can talk it through.

@bhaugen
bhaugen commented Mar 25, 2014

Gotta run an errand so I can't jump on django now, but thanks for the tip. Here's what showed:


PIL SETUP SUMMARY
--------------------------------------------------------------------
version      Pillow 2.3.1
platform     linux2 2.7.3 (default, Feb 27 2014, 19:58:35)
             [GCC 4.6.3]
--------------------------------------------------------------------
*** TKINTER support not available
*** JPEG support not available
--- ZLIB (PNG/ZIP) support available
*** LIBTIFF support not available
*** FREETYPE2 support not available
*** LITTLECMS2 support not available
*** WEBP support not available
*** WEBPMUX support not available
--------------------------------------------------------------------
To add a missing option, make sure you have the required
library, and set the corresponding ROOT variable in the
setup.py script.

Wasn't paying enough attention. I'll study the Pillow doc, they probably have some useful info.

@bhaugen
bhaugen commented Mar 26, 2014

Pillow doc was good: http://pillow.readthedocs.org/en/latest/installation.html#simple-installation
Most of the libraries they suggested installed according to their instructions.
libtiff4-dev failed but I don't care. Easy-thumbnails now handles jpegs without complaint, and without using PIL.
@aclark4life thanks again for setting me straight.

@aclark4life

@bhaugen Glad to hear!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.