watermark

 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- coding: UTF-8 -*-

import Image, ImageEnhance

from utils.consts import MARK_IMG, PADDING

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

def watermark(img, mark_img = MARK_IMG, position = 'bottom-right', opacity = 0.6):
    """Adds a watermark to an image."""

    img = Image.open(img)
    mark = Image.open(mark_img)
    
    img_w_p = img.size[0] - PADDING
    if img_w_p < mark.size[0]:
        ratio = float(img_w_p) / mark.size[0]
        w = int(mark.size[0] * ratio)
        h = int(mark.size[1] * ratio)
        mark = mark.resize((w, h))
    
    if opacity < 1:
        mark = reduce_opacity(mark, opacity)
    
    if img.mode != 'RGBA':
        img = img.convert('RGBA')
    
    # create a transparent layer the size of the image and draw the watermark in that layer.
    layer = Image.new('RGBA', img.size, (0,0,0,0))
    
    if position == 'over':
        for y in xrange(0, img.size[1], mark.size[1]):
            for x in xrange(0, img.size[0], mark.size[0]):
                layer.paste(mark, (x, y))
    elif position == 'title':
        # title, but preserve the aspect ratio
        ratio = min(float(img.size[0]) / mark.size[0], float(img.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, ((img.size[0] - w) / 2, (img.size[1] - h) / 2))
        layer.paste(mark, ((img.size[0] - w) / 2, 0))
    elif position == 'top-left':
        position = (PADDING, PADDING)
        layer.paste(mark, position)
    elif position == 'top-right':
        position = (img.size[0] - mark.size[0] - PADDING, PADDING)
        layer.paste(mark, position)
    elif position == 'center':
        position = ((img.size[0] - mark.size[0])/2, (img.size[1] - mark.size[1])/2)
        layer.paste(mark, position)
    elif position == 'bottom-left':
        position = (PADDING, img.size[1] - mark.size[1]  -PADDING,)
        layer.paste(mark, position)
    else: # 'bottom-right' (default)
        position = (img.size[0] - mark.size[0] - PADDING, img.size[1] - mark.size[1] - PADDING,)
        layer.paste(mark, position)
        
    return Image.composite(layer, img, layer)

def test():
    watermark('3.jpg',MARK_IMG,'LEFT_TOP',opacity=0.7).save("watermarked_lt.jpg",quality=90)
    watermark('3.jpg',MARK_IMG,'RIGHT_TOP',opacity=0.7).save("watermarked_rt.jpg",quality=90)
    watermark('3.jpg',MARK_IMG,'CENTER',opacity=0.7).save("watermarked_center.jpg",quality=90)
    watermark('3.jpg',MARK_IMG,'LEFT_BOTTOM',opacity=0.7).save("watermarked_lb.jpg",quality=90)
    watermark('3.jpg',MARK_IMG,'RIGHT_BOTTOM',opacity=0.7).save("watermarked_rb.jpg",quality=90)
    watermark('3.jpg',MARK_IMG,'OVER',opacity=0.7).save("watermarked_o.jpg",quality=90)
    watermark('3.jpg',MARK_IMG,'TITLE',opacity=0.7).save("watermarked_title.jpg",quality=90)
    
if __name__ == '__main__':
    test()  

More like this

  1. . by jeremydw 3 years, 8 months ago
  2. madslug by catellar 2 years, 8 months ago
  3. convert youtube url for iframe, js api by ivanff 1 year ago
  4. default url routing and shortcut by vicalloy 4 years, 10 months ago
  5. Templatetag to manage GET arguments in template by DimmuR 3 years, 5 months ago

Comments

(Forgotten your password?)