-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradient_transfer.py
69 lines (57 loc) · 2.47 KB
/
gradient_transfer.py
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
from PIL import Image
def getGradientMap(image, scale=1, justcompare=False):
gradient = image.resize((int(scale * image.width), int(scale * image.height))).convert('RGBA')
gradienti = gradient.convert('L')
tally = {}
for (r, g, b, a), i in zip(gradient.getdata(), gradienti.getdata()):
if a > 0:
tally.setdefault(i, {})
tally[i].setdefault((r, g, b), 0)
tally[i][(r, g, b)] += a
sparsemap = {}
for i in tally:
t, h, n, w = 0, 0, 0, 0
for r, g, b in tally[i]:
weight = tally[i][(r, g, b)] # weight = 1 is smoother but more faded
t += r * weight
h += g * weight
n += b * weight
w += weight
sparsemap[i] = (int(t / w), int(h / w), int(n / w))
sparsemap.setdefault(0, (0, 0, 0))
sparsemap.setdefault(255, (255, 255, 255))
power = 1 + len(sparsemap) / 4 # 1 - 65
gradientmap = []
for i in range(256):
if i in sparsemap:
gradientmap.append(sparsemap[i])
else:
t, h, n, w = 0, 0, 0, 0
for j in sparsemap:
r, g, b = sparsemap[j]
weight = (255 - abs(j - i)) ** power
t += r * weight
h += g * weight
n += b * weight
w += weight
gradientmap.append((int(t / w), int(h / w), int(n / w)))
if justcompare:
swatch = Image.new('RGB', (256, 2), (0, 255, 0))
swatch.putdata(gradientmap)
for i in sparsemap:
swatch.putpixel((i, 1), (*sparsemap[i], 255))
return swatch.resize((256, 256), Image.NEAREST)
return gradientmap
def applyGradientMap(image, gradientmap, scale=1):
img = image.resize((int(scale * image.width), int(scale * image.height))).convert('RGBA')
imgi = img.convert('L')
img.putdata([(*gradientmap[i], a) for (r, g, b, a), i in zip(img.getdata(), imgi.getdata())])
return img
if __name__ == '__main__':
pinkmap = getGradientMap(Image.open('./input/GiftIcon_Standard.png'), 2)
goldmap = getGradientMap(Image.open('./input/GiftIcon_Gold.png'), 2)
molly = Image.open('./input/molly.png')
applyGradientMap(molly, pinkmap).save('./output/mollypink.png')
applyGradientMap(molly, goldmap).save('./output/mollygold.png')
# applyGradientMap(Image.open('./input/pol.png'), goldmap).save('./output/polgold.png')
# getGradientMap(Image.open('./input/2x2.png'), 8, justcompare=True) # super sparse test