-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathvicariouswatermark.py
More file actions
60 lines (54 loc) · 1.79 KB
/
vicariouswatermark.py
File metadata and controls
60 lines (54 loc) · 1.79 KB
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
from PIL import Image, ImageColor
import numpy as np
import random
import vicariousnetwork
def adjustcolor(imInput, fromRGB, toRGB):
im = imInput.convert('RGBA')
data = np.array(im)
red,green,blue,alpha = data.T
colorset = (red==fromRGB[0]) & (green==fromRGB[1]) & (blue==fromRGB[2])
data[...,:-1][colorset.T]=toRGB
imOutput = Image.fromarray(data)
return imOutput
def getrandomcolor():
r=random.randint(48,255)
g=random.randint(48,255)
b=random.randint(48,255)
return (r,g,b)
def adjustalpha(imInput, reducealpha):
if reducealpha == 0:
return imInput
# todo: adjust alpha up or down
im = imInput.convert('RGBA')
data = np.array(im)
data[:,:,3] -= abs(reducealpha) # 64 # alpha
imOutput = Image.fromarray(data)
return imOutput
def resizeToWidth(imInput, desiredWidth):
w = imInput.width
h = imInput.height
if w == desiredWidth:
return imInput
else:
r = desiredWidth / w
nw = int(w * r)
nh = int(h * r)
imOutput = imInput.resize((nw,nh))
return imOutput
svgfile = None
def getsvgfile():
global svgfile
if svgfile is None:
svgfile = vicariousnetwork.getimagefromurl(True,"file://../images/nodeyez.svg")
return svgfile
def do(canvas, width=80, box=(0,0), reducealpha=0, recolor=True, recolortorandom=True, recolorfrom=(102,102,102), recolorto=(255,255,255)):
wm = getsvgfile()
if recolor == True:
if recolortorandom == True:
recolorto=getrandomcolor()
wm = adjustcolor(wm, recolorfrom, recolorto)
wm = adjustalpha(wm, reducealpha)
wm = resizeToWidth(wm, width)
wmlayer = Image.new(mode="RGBA", size=(canvas.width, canvas.height), color=(00,00,00,00))
wmlayer.paste(wm, box)
canvas.alpha_composite(wmlayer)