Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@yuchant
Created February 27, 2012 01:29
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuchant/1920535 to your computer and use it in GitHub Desktop.
Save yuchant/1920535 to your computer and use it in GitHub Desktop.
Sorl Thumbnail PIL Engine that accepts PNG conversion to JPG with background color
"""
Sorl Thumbnail Engine that accepts background color
---------------------------------------------------
Created on Sunday, February 2012 by Yuji Tomita
"""
from PIL import Image, ImageColor
from sorl.thumbnail.engines.pil_engine import Engine
class Engine(Engine):
def create(self, image, geometry, options):
thumb = super(Engine, self).create(image, geometry, options)
if options.get('background'):
try:
background = Image.new('RGB', thumb.size, ImageColor.getcolor(options.get('background'), 'RGB'))
background.paste(thumb, mask=thumb.split()[3]) # 3 is the alpha of an RGBA image.
return background
except Exception, e:
return thumb
return thumb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment