Created
February 27, 2012 01:29
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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