A friend was facing problems regarding some artistic photos being copied from his website without any credits, and was wondering what to do. One of the suggestions was including a diagonal watermark in the high resolution pictures preventing people from easily copying photos without actually contacting him.
I present below a small Python program which uses the Python Imaging Library to build a diagonal textual watermark. It will adapt the generated mark size to the text and image provided.
Here is a sample of how the watermark looks like:
from PIL import Image, ImageDraw, ImageFont from math import atan, degrees import sys import os FONT = "/usr/X11R6/lib/X11/fonts/TTF/Vera.ttf" def main(filename, text, outfilename): img = Image.open(filename).convert("RGB") watermark = Image.new("RGBA", (img.size, img.size)) draw = ImageDraw.ImageDraw(watermark, "RGBA") size = 0 while True: size += 1 nextfont = ImageFont.truetype(FONT, size) nexttextwidth, nexttextheight = nextfont.getsize(text) if nexttextwidth+nexttextheight/3 > watermark.size: break font = nextfont textwidth, textheight = nexttextwidth, nexttextheight draw.setfont(font) draw.text(((watermark.size-textwidth)/2, (watermark.size-textheight)/2), text) watermark = watermark.rotate(degrees(atan(float(img.size)/ img.size)), Image.BICUBIC) mask = watermark.convert("L").point(lambda x: min(x, 55)) watermark.putalpha(mask) img.paste(watermark, None, watermark) img.save(outfilename) if __name__ == "__main__": if len(sys.argv) != 4: sys.exit("Usage: %s <input-image> <text> <output-image>" % os.path.basename(sys.argv)) main(*sys.argv[1:])