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
- find even number by Rajeev529 2 months, 3 weeks ago
- Form field with fixed value by roam 3 months, 2 weeks ago
- New Snippet! by Antoliny0919 3 months, 3 weeks ago
- Add Toggle Switch Widget to Django Forms by OgliariNatan 6 months, 1 week ago
- get_object_or_none by azwdevops 10 months ago
Comments
Did not work for me. jpg thumbs were bad, png thumbs did not resize.
#
Please login first before commenting.