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
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Did not work for me. jpg thumbs were bad, png thumbs did not resize.
#
Please login first before commenting.