[Tilecache] Watermarking Tiles in TileCache

Andrew Hughes azza at lisasoft.com
Tue May 1 00:16:52 EDT 2007


Hi All,

Some tile sources do not provide water marking of their images... or 
even very simple text based watermarking that looks terrible. Firstly, 
this is is not difficult to implement and I have attatched python code 
that does just this. Even I know very little python but the attached 
code does work.

I understand that this is bespoke functionality, however I would like to 
offer it to the community/MetaCarta as a contribution. Integration of 
this into TileCache could be very easy.... Suggestively the [Layer] 
would have a new non-mandatory parameter 
"watermarkImage=/path/to/some/image.png" (a 256 x 256 image as default). 
In the presence of this parameter each tile (after any metaTile has been 
cut up) has this watermark applied with the attatched code, and 
persisted to cache with watermark.

This is low cost functionality that shouldn't introduce any new 
maintainance issues, and I would be happy to contribute if a Sandbox is 
made available. I would rather not diverge, it's a last resort.


--AH

------------------------------------------------------------------------------------------------------------------------------------------------

import Image, ImageEnhance

def reduce_opacity(im, opacity):
    """Returns an image with reduced opacity."""
    assert opacity >= 0 and opacity <= 1
    if im.mode != 'RGBA':
        im = im.convert('RGBA')
    else:
        im = im.copy()
    alpha = im.split()[3]
    alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
    im.putalpha(alpha)
    return im

def watermark(im, mark, position, opacity=1):
    """Adds a watermark to an image."""
    if opacity < 1:
        mark = reduce_opacity(mark, opacity)
    if im.mode != 'RGBA':
        im = im.convert('RGBA')
    layer = Image.new('RGBA', im.size, (0,0,0,0))
    layer.paste(mark, position)
    return Image.composite(layer, im, layer)

def test():
    im = Image.open('Input.jpeg')
    mark = Image.open('Watermark.png')
    watermark(im, mark, (0,0), 0.5).save('Output.jpeg')

if __name__ == '__main__':
    test()







More information about the Tilecache mailing list