Login

sorl.thumbnail processor: white background

Author:
izibi
Posted:
August 4, 2008
Language:
Python
Version:
.96
Tags:
png background sorl.thumbnail
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

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.