Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InvalidImageFormatError: The source file does not appear to be an image #290

Closed
robpodosek opened this issue Jan 10, 2014 · 16 comments
Closed

Comments

@robpodosek
Copy link

I'm getting the following error when trying to output a thumbnail in my Django template:

InvalidImageFormatError at /search/
The source file does not appear to be an image

This is happening with both JPEGs and PNGs. Here's how my template looks:

{% load thumbnail %}
{% thumbnail profile.profile_photo 220x160 %}

I have THUMBNAIL_DEBUG = True in my settings file and easy_thumbnails added to my installed apps.

In my requirements.txt:

Pillow==2.2.1
easy-thumbnails==1.4

And before people say I don't have png/jpeg support with Pillow/PIL here's a pip install -r requirements.txt:

Installing collected packages: PIL
  Running setup.py develop for PIL

    --------------------------------------------------------------------
    PIL 1.2a0 SETUP SUMMARY
    --------------------------------------------------------------------
    version       1.2a0
    platform      Python 2.7.3 (default, Apr 10 2013, 06:20:15)
                  [GCC 4.6.3] on linux2
    --------------------------------------------------------------------
    --- TKINTER support available
    --- JPEG support available
    *** WEBP support not available
    --- ZLIB (PNG/ZIP) support available
    --- FREETYPE2 support available
    *** LITTLECMS 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.

    To check the build, run the selftest.py script.
    Creating /home/robbie/git/myproject/venv/lib/python2.7/site-packages/PIL.egg-link (link to .)
    PIL 1.2a0 is already the active version in easy-install.pth

So JPEG and PNG support are available. I'm also able to access the uploaded image from the browser by pointing to it's MEDIA_URL path. What gives?

edit:
I also get the same error when trying to load a 3rd party image such as

{% thumbnail "http://www.gravatar.com/avatar/00000000000000000000000000000000?d=mm" 160x160 %}
@katzlbt
Copy link

katzlbt commented Jan 12, 2014

In my experience, this error is produced for about everything including permission denied, file not found, PIL not working.

@SmileyChris
Copy link
Owner

Yes, see #221 on making this better.
Have you tried manually opening one of the problematic images in PIL in a shell on the offending system?

@robpodosek
Copy link
Author

Opening it from PIL in the django shell inside my virtualenv works just fine.

In [1]: from PIL import Image

In [2]: Image.open("/home/robbie/image.jpg")
Out[2]: <PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1024x1536 at 0x3E68710>

I'm more concerned that the thumbnail tag isn't even able to load 3rd party images like when I do

{% thumbnail "http://www.gravatar.com/avatar/00000000000000000000000000000000?d=mm" 160x160 %}

@SmileyChris
Copy link
Owner

@robpodosek did you find the problem?

@SmileyChris
Copy link
Owner

#259 is tracking being able to load thumbnails from a URL (it's not a very efficient thing to do, but should be able to be implemented without that much fuss)

@alep
Copy link

alep commented Feb 19, 2014

@robpodosek what was the problem? I'm having a similar issue.

@JensDebergh
Copy link

@robpodosek Any hints on solving this? I'm having the same issue right now.

@oesah
Copy link

oesah commented Feb 2, 2017

This error should be silenced. It breaks the entire page, if an image has some form of damage or is incorrect. Instead, you should display a warning text or something similar. But breaking the entire page is too much for a simple error like this....

@oesah
Copy link

oesah commented Feb 2, 2017

And the error appears for different reason. The basic reason is, easy thumbnails have problems processing the image. That could have many reasons. I just find, raising a page-breaking error is not the solution to this. They need to silence it and let the user know, whats wrong with the image.

@Luc45
Copy link

Luc45 commented Aug 14, 2017

This happens to me as well, and is taking down the entire site with 2m monthly views. There should be a fallback image to avoid error 500.

@MaykelLlanes
Copy link

The exact same things are happening to me.
So I am using fallback default image to avoid 500 error and it is not successful.
I keep getting the same error "InvalidImageFormatError: The source file does not appear to be an image".
In my case I try to thumbnail an image that does not exist.
But it is assumed that if the image does not exist it should use the one that is in default as fallback.
Any ideas? Someone who has happened in the same way and found a solution?
In my case this is my code {% oscar_thumbnail child_image.original|default:'image/default.png' "450x450" format="WEBP" padding=True as thumb_child %}

Thanks in advance.

@jrief
Copy link
Collaborator

jrief commented Nov 19, 2020

@MaykelLlanes could you please send me that image (if you can/use my private email) and tell me how you upload and access it.

You are using a templatetag (oscar_thumbnail ) I'm not familiar with, how does that error look like, if you use the native thumbnail templatetag provided with easy-thumbnails?

@MaykelLlanes
Copy link

MaykelLlanes commented Nov 19, 2020

Thanks @jrief for the quick reply
In my case I am migrating from sorl-thumbnails which comes by default with django_oscar for easy_thumbnails.
The tag OSCAR_THUMBNAIL is used by django_oscar and oscar replaces it with the thumbnailer that is in settings in this case easy_thumbnails. I have debugged the code, and it is entering the easy_thumb classes well ... and the parameters are arriving correctly.
The thing is, it doesn't make me fallback when the image it's trying to process doesn't exist.
I have a database with products that have associated images, many of these do not exist in my local environment, because in production they are in s3, and I do not have my settings configured for s3 in my development environment. But hey, the question is that it should fallback when there is no image, or there is a reference to an image that does not physically exist. With which I intend to do fallback if it exists but it keeps giving me error 500 and it does not show me the page, I get the django page with the exceptions. Oh and I've also tried the native easy_thumb tag ...

image

@jrief
Copy link
Collaborator

jrief commented Nov 19, 2020

Could you please send me that image so that I can test in my environment. Maybe PIL is unable to convert CR01-71.jpg to WEBP?
You can also try to turn off the fail_silenty option. This might give you a more verbose exception message.

@MaykelLlanes
Copy link

The problem was the following, for the default filter to take the value the previous expression must be evaluated to False, for this reason it explodes, because there is no image, the thumbnailer cannot create the object, the solution is to make another filter to check if the image exists in the storage, and return a boolean value.

{% oscar_thumbnail child_image.original|image_exist|default:'image/default.png' "450x450" format="WEBP" padding=True as thumb_child %}

Thanks.

@Joel-Edem
Copy link

Joel-Edem commented Apr 19, 2022

I recently had a similar issue, and i thought I'd share how i resolved my issue. Easy thumbnails uses easy_thumbnails.storage.ThumbnailFileSystemStorage as default storage set via THUMBNAIL_DEFAULT_STORAGE . This is usually fine during development and I change that for production. However during deployment if this value is not set properly the thumbnails will be generated using the default ThumbnailFileSystemStorage storage option however if you serve media files from a different location thumbnails cant locate the source files or thinks it has a thumbnail generated but cant find it , and throws the error. You can suppress this by setting THUMBNAIL_DEBUG to False while you diagnose the problem. if youve changed your DEFAULT_FILE_STORAGE or STATICFILES_STORAGE or have set the storages option on a model field, you might have to configure THUMBNAIL_DEFAULT_STORAGE to find and serve the files properly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants