Welcome, guest | Sign In | My Account | Store | Cart

Apply a watermark to an image using the Python Imaging Library. Supports color, tiling, scaling, and opacity reduction.

Python, 49 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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')
    # create a transparent layer the size of the image and draw the
    # watermark in that layer.
    layer = Image.new('RGBA', im.size, (0,0,0,0))
    if position == 'tile':
        for y in range(0, im.size[1], mark.size[1]):
            for x in range(0, im.size[0], mark.size[0]):
                layer.paste(mark, (x, y))
    elif position == 'scale':
        # scale, but preserve the aspect ratio
        ratio = min(
            float(im.size[0]) / mark.size[0], float(im.size[1]) / mark.size[1])
        w = int(mark.size[0] * ratio)
        h = int(mark.size[1] * ratio)
        mark = mark.resize((w, h))
        layer.paste(mark, ((im.size[0] - w) / 2, (im.size[1] - h) / 2))
    else:
        layer.paste(mark, position)
    # composite the watermark with the layer
    return Image.composite(layer, im, layer)

def test():
    im = Image.open('test.png')
    mark = Image.open('overlay.png')
    watermark(im, mark, 'tile', 0.5).show()
    watermark(im, mark, 'scale', 1.0).show()
    watermark(im, mark, (100, 100), 0.5).show()

if __name__ == '__main__':
    test()

Use the watermark function to blend a watermark into an image. The image and watermark are PIL images. The position is either an (x,y) tuple, "tile", or "scale". The opacity is a number between 0 and 1; lower values make the watermark less intrusive and less visible. The watermark function uses the reduce_opacity function, but if you're applying a watermark to many images, you can use the reduce_opacity function to premultiply the watermark.

This code requires PIL 1.1.4. It is fast, but it may consume a large amount of memory when working with large images.

This recipe is one answer to a post on the Python list:

http://mail.python.org/pipermail/python-list/2004-November/251149.html

5 comments

Zoom Quiet 19 years, 2 months ago  # | flag

error when running... """raise ImportError("The _imaging C module is not installed") ImportError: The _imaging C module is not installed """ i'm test under win2k3, ActivePython 2.4 Build 243 + PIL-1.1.4.win32-py2.3

"C module" what is ??

Shane Hathaway (author) 19 years, 2 months ago  # | flag

PIL might not be fully installed. The problem is at a lower level than I can solve easily, but it sounds like PIL is not installed correctly. Try reinstalling PIL, then test it using an interactive prompt, like this:

&gt;&gt;&gt; import Image
&gt;&gt;&gt; Image.open('overlay.png').show()
Damjan Georgievski 18 years, 12 months ago  # | flag

ActivePython 2.4 Build 243 + PIL-1.1.4.win32-py2.3

You are using Python 2.4 with PIL compiled for Python-2.3. That will not work.

Terry Carroll 18 years, 10 months ago  # | flag

Great for adding dates on digital photos, too. Shane, thanks for this recipe. I needed to put together a routine to add an imprint of a date to digital photos (my wife likes the date to appear, but I don't; so this keeps us both happy), and your recipe gave me a great starting point.

def Imprint(im, inputtext, font=None, color=None, opacity=.6, margin=(30,30)):
    """
    imprints a PIL image with the indicated text in lower-right corner
    """
    if im.mode != "RGBA":
        im = im.convert("RGBA")
    textlayer = Image.new("RGBA", im.size, (0,0,0,0))
    textdraw = ImageDraw.Draw(textlayer)
    textsize = textdraw.textsize(inputtext, font=font)
    textpos = [im.size[i]-textsize[i]-margin[i] for i in [0,1]]
    textdraw.text(textpos, inputtext, font=font, fill=color)
    if opacity != 1:
        textlayer = reduce_opacity(textlayer,opacity)
    return Image.composite(textlayer, im, textlayer)

I use the following to get the date to be added:

def GetFileDate(file):
    """
    Returns the date associated with a file.
    For JPEG files, it will use the EXIF data, if available
    """
    try:
        import EXIF
        # EXIF.py from http://home.cfl.rr.com/genecash/digital_camera.html
        f = open(file, "rb")
        tags = EXIF.process_file(f)
        f.close()
        return str(tags['Image DateTime'])
    except (KeyError, ImportError):
        # EXIF.py not installed or no EXIF date available
        import os.path, time
        return time.ctime(os.path.getmtime(file))
hasanatkazmi 14 years, 9 months ago  # | flag

Thanks, this peace of code helped me alot, But i wanted to put watermarks in bath, putting watermarks on multiple photos. Here is what I created out of this. http://hasanatkazmi.blogspot.com/2009/06/putting-watermark-to-images-in-batch.html