Last active
December 15, 2015 19:18
-
-
Save bmihelac/5309916 to your computer and use it in GitHub Desktop.
easy-thumbnails watermark to right bottom corner of the image
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
# custom/processors.py | |
from django.conf import settings | |
from django.core.files.storage import default_storage | |
try: | |
from PIL import Image | |
except ImportError: | |
import Image | |
def watermark(image, watermark=False, position=False, **kwargs): | |
if watermark: | |
watermark_conf = settings.THUMBNAIL_WATERMARK.get(watermark) | |
name = watermark_conf['name'] | |
storage = watermark_conf.get('storage', default_storage) | |
watermark_image = Image.open(storage.open(name)) | |
if image.mode != 'RGBA': | |
image = image.convert('RGBA') | |
layer = Image.new('RGBA', image.size, (0, 0, 0, 0)) | |
width, height = image.size | |
x = width - watermark_image.size[0] | |
y = height - watermark_image.size[1] | |
layer.paste(watermark_image, (x, y)) | |
image = Image.composite(layer, image, layer) | |
return image |
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
THUMBNAIL_ALIASES = { | |
'': { | |
'thumb': { | |
'size': (320, 240), | |
'watermark': 'logo', | |
} | |
} | |
} | |
THUMBNAIL_WATERMARK = { | |
'logo': { | |
'name': 'logo.png', | |
# optional storage class | |
# 'storage': mystorage, | |
} | |
} | |
THUMBNAIL_PROCESSORS = ( | |
'easy_thumbnails.processors.colorspace', | |
'easy_thumbnails.processors.autocrop', | |
'easy_thumbnails.processors.scale_and_crop', | |
'easy_thumbnails.processors.filters', | |
'custom.processors.watermark', | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment