-
Notifications
You must be signed in to change notification settings - Fork 11
/
test_local_wrap.py
111 lines (85 loc) · 2.82 KB
/
test_local_wrap.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
'''
lanhuage: python
Descripttion: https://github.com/cedricporter/EffectLab/blob/master/EffectLab/Effect.py
version: beta
Author: xiaoshuyui
Date: 2020-10-23 11:03:38
LastEditors: xiaoshuyui
LastEditTime: 2020-10-23 13:44:42
'''
import operator
from math import sqrt
import numpy as np
from PIL import Image
def warp(x, y, r, center: tuple = None, mouse: tuple = None):
if center is None:
center = (130, 120)
if mouse is None:
mouse = (130, 50)
cx, cy = center
mx, my = mouse
dis_x_c = sqrt((x - cx)**2 + (y - cy)**2)
dis_m_c = sqrt((x - mx)**2 + (y - my)**2)
div = float(r**2 - dis_x_c**2 + dis_m_c**2)
if div == 0:
div = 0.0000000001
factor = ((r**2 - dis_x_c**2) / div)**2
u = x - factor * (mx - cx)
v = y - factor * (my - cy)
return u, v
def _div(t: list, v):
tmp = np.array(t)
tmp = tmp / v
return tmp.astype(np.uint8).tolist()
def imgFilter(img: Image,
radius: int = 100,
center: tuple = None,
mouse: tuple = None,
antialias=2):
width, height = img.size
new_img = img.copy()
r = radius
if center is None:
center = (130, 120)
if mouse is None:
mouse = (130, 50)
cx, cy = center
mx, my = mouse
nband = len(img.getpixel((0, 0)))
antialias = antialias
for x in range(width):
for y in range(height):
if sqrt((x - cx)**2 + (y - cy)**2) > r:
continue
found = 0
psum = (0, ) * nband
# anti-alias
for ai in range(antialias):
_x = x + ai / float(antialias)
for aj in range(antialias):
_y = y + aj / float(antialias)
u, v = warp(_x, _y, r, (cx, cy), (mx, my))
u = int(round(u))
v = int(round(v))
if not (0 <= u < width and 0 <= v < height):
continue
pt = img.getpixel((u, v))
psum = map(operator.add, psum, pt)
found += 1
if found > 0:
# print((found, ) * len(list(psum)))
psum = _div(list(psum), found)
# print(psum)
# psum = map(operator.truediv, list(psum), (found, ) * len(list(psum)))
new_img.putpixel((x, y), tuple(psum))
return new_img
if __name__ == "__main__":
# img = Image.open(
# "D:\\testALg\\mask2json\\mask2json\\static\\multi_objs.jpg")
# res = imgFilter(img)
# res.save("D:\\testALg\\mask2json\\mask2json\\static\\multi_objs_wrap.jpg")
import sys
sys.path.append('..')
from convertmask.utils.auglib.optional.distort import imgDistort
img = "D:\\testALg\\mask2json\\mask2json\\static\\multi_objs.jpg"
imgDistort(img)