Login

sorl.thumbnail processor: white background

Author:
izibi
Posted:
August 4, 2008
Language:
Python
Version:
.96
Score:
0 (after 0 ratings)

If you want to resize transparent PNGs with the {% thumbnail %} templatetag, they'll sometimes get an ugly black background that looks even more ugly on a white background. This processor puts the image on a white background. You can simply change the background color by replacing white with any other color.

To use this filter simple put the following two lines of code in your settings file:

from sorl.thumbnail.defaults import PROCESSORS as THUMBNAIL_PROCESSORS
THUMBNAIL_PROCESSORS = ('path.to.white_background',) + THUMBNAIL_PROCESSORS
1
2
3
4
5
6
7
8
from PIL import Image, ImageColor
def white_background(im, requested_size, opts):
    try:
        background = Image.new('RGB', im.size, ImageColor.getcolor('white', 'RGB'))
        background.paste(im, mask=im.split()[3])
        return background
    except:
        return im

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

xhenxhe (on July 24, 2010):

Did not work for me. jpg thumbs were bad, png thumbs did not resize.

#

Please login first before commenting.