X-Git-Url: https://scm.cri.ensmp.fr/git/Portfolio.git/blobdiff_plain/2da869b2b46179d64c05eaac4081226fdbbbc9ff..e73353500c67453ef9663d67eaeee7dd46c358ee:/Products/Portfolio/Extensions/watermark.py diff --git a/Products/Portfolio/Extensions/watermark.py b/Products/Portfolio/Extensions/watermark.py new file mode 100755 index 0000000..871eb9d --- /dev/null +++ b/Products/Portfolio/Extensions/watermark.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +############################################################ +# Copyright © 2005-2008 Benoît PIN # +# Plinn - http://plinn.org # +# # +# This program is free software; you can redistribute it # +# and/or modify it under the terms of the Creative Commons # +# "Attribution-Noncommercial 2.0 Generic" # +# http://creativecommons.org/licenses/by-nc/2.0/ # +############################################################ +""" + + +""" + +from Globals import package_home +from os import path +from PIL.Image import open as imgopen +from ImageDraw import Draw +from PIL.ImageDraw import Draw +from PIL.ImageFont import truetype +from Products.Portfolio.Extensions import fontdir +from Products.Photo.cache import memoizedmethod + +class FontCache: + def __init__(self) : + self._methodResultsCache = {} + + @memoizedmethod('name', 'size', 'index') + def get(self, name, size, index) : + fontfile = path.join(fontdir, name) + font = truetype(fontfile, size, index=index) + return font + + +fontCache = FontCache() + + +def addWatermark(self, image, text, size=20, margin_right=10, margin_bottom=10, font='verdana.ttf', index=0, encoding='utf-8') : + + font = fontCache.get(font, size, index) + text = text.decode(encoding) + tw, th = font.getsize(text) + iw, ih = image.size + + d = Draw(image) + + left, top = (iw - tw - margin_right, ih - th - margin_bottom) + + d.text((left, top), text, font = font)