8

I'm developing my project inside a Vagrant VM, the software version I'm using is:

  • Ubuntu 12.04
  • Django 1.6
  • Sorl Thumbnail 11.12
  • Pillow 2.5.3

I have some pictures in the path /var/www/django/my_project/media/icons and I have a model with an ImageField pointing to that path.

I have also THUMBNAIL_DEBUG = True in my settings.py

In my template I use thumbnail like:

{% thumbnail category.image "20" as im %}
    <img src="{{ im.url }}"></img>
{% empty %}
    {% thumbnail "png/no_image.png" "20" as im %}  # Thumbnail add the rest of the path to media
        <img id="no_image" alt="" src="{{ im.url }}" />
    {% endthumbnail%}
{% endthumbnail %}

There are some objects with image and some others without it and both shows just a black square instead of the image.

  • Does anyone knows why is this happening ? I use Sorl-Thumbnail in many projects and never had this issue

I've tried to reset the full database, I used python manage.py thumbnail clear and python manage.py thumbnail cleanup

I've installed: libjpeg62 libjpeg62-dev zlib1g-dev

no_image.png is an image (red cross) not an empty image

  • Am I missing any library or dependency ?

I'm lost because as I said I've worked with Sorl-thumbnail in other projects, and never saw anything like this.

Any help would be much appreciated


Edit

Here are some images I'm trying to display:

enter image description here enter image description here

( I have png and jpg libraries working on Pillow )

And this is what I see in all cases:

enter image description here

Edit2

The problem is with the background. When I upload a png image with transparent background sorl converts the image to jpg and set the background to black...

  • Is there any way to keep the transparent background or avoid conversion ?
  • Is possible to set the background to white ?

I tried with:

THUMBNAIL_COLORSPACE = None
THUMBNAIL_PRESERVE_FORMAT = True

but didn't work

Community
  • 1
  • 1
AlvaroAV
  • 10,335
  • 12
  • 60
  • 91
  • When you say 'black square', is that the `no_image.png`? When you look at the actual path being generate in the `src` for the image, is it pointing to the sorl thumbnail cache path (something like `/media/cache/ab/fa/abf...e08.jpg`) i.e. is sorl actually creating the thumbnails? – Timmy O'Mahony Nov 05 '14 at 22:23
  • @TimmyO'Mahony The image no_image.png is a green circle and sor-thumbnail is generating the images in `/media/cache/ab/...` but this images are just black squares, it doesn't matter the contain on the image, sorl-thumbnail generates a black picture – AlvaroAV Nov 06 '14 at 07:38
  • Have you checked that `Pillow` installed with `png` support, `ZLIB (PNG/ZIP) support available` because when I used ubuntu there was problems with path to `zlib1g-dev` and others, so I resolved it `sudo ln -s /usr/lib/x86_64-linux-gnu/libz.so /usr/lib` and same for other libs, then `pip install -U Pillow` – madzohan Nov 12 '14 at 16:42
  • @madzohan I think the issue you're saying is for x64 machines and my machine is 32, anyways I checked Pillow installation and it says `ZLIB (PNG/ZIP) support available` – AlvaroAV Nov 13 '14 at 07:30
  • Do you have an example of a image that you tried to make a thumbnail of and the result? Have you set any other sorl-thumbnail related settings? – relekang Nov 14 '14 at 06:31
  • @relekang the issue happens with all images, I show you here 2 of them. And I allways see the black square – AlvaroAV Nov 14 '14 at 07:38
  • Works for me with your requirements. See a sample app: https://bitbucket.org/barseghyanartur/sorl-thumbnail-generates-black-square-instead-of-image – Artur Barseghyan Nov 17 '14 at 13:14
  • In settings.py is `DEBUG = False`? Or, do you have any normal images (not run through Thumbnail) that are showing correctly? – jcfollower Jan 07 '15 at 20:43
  • @jcfollower it seems the issue is with the background, sorl is changing the transparent background to black background because the png images are converted to jpg... I don't know if maybe PIL could be better than Pillow for this – AlvaroAV Jan 07 '15 at 20:47

2 Answers2

11

Finally solved !

To solve the issue with the black background:

  • I updated sorl-thumbnail to 12.2
  • Added this 2 lines to settings.py:
    • THUMBNAIL_COLORSPACE = None
    • THUMBNAIL_PRESERVE_FORMAT = True

  • Restart thumbnail database with python manage.py thumbnail clear_delete_all
AlvaroAV
  • 10,335
  • 12
  • 60
  • 91
  • I was still facing issues after this (`OSError: cannot write mode P as JPEG`), I ended up using these settings (since I don't care much about the image compression) `THUMBNAIL_PRESERVE_FORMAT = True THUMBNAIL_FORMAT = 'PNG'`. Did not change the `THUMBNAIL_COLORSPACE` setting (defaults to `'RGB'`). – shad0w_wa1k3r May 13 '18 at 12:52
0

It seems like a bug in that version of sorl-thumbnail related to bad meta-data. It is currently fixed in the release candidate currently on pypi. Since pip does not install RC by default you have to specify the version to install it. Try:

pip install sorl-thumbnail==12.1c

I tried it out with the python logo and it worked fine for me with 12.1c

relekang
  • 1,844
  • 16
  • 6
  • It is weird, because actually I have almost same env: `Django 1.6.7, Sorl Thumbnail 11.12, Pillow 2.5.3` except `Ubuntu`, I am on `OS X` and everything work smoothly – madzohan Nov 14 '14 at 18:16
  • 1
    It's likely that you have either PIL or Pillow installed in your system (not in the virtualenv). You could try to recreate your virtual environment with --no-site-packages option and then install Pillow. – Artur Barseghyan Nov 19 '14 at 08:09