-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathMTCore.py
127 lines (95 loc) · 3.91 KB
/
MTCore.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from PIL import Image
from PIL import ImageEnhance
# 感谢老司机
# https://zhuanlan.zhihu.com/p/31164700
def grayCar(whiteImg, blackImg, light=0.3, chess=False):
"""发黑白车"""
# 加载图像, 转成灰度图
_im1 = Image.open(whiteImg).convert('L')
_im2 = Image.open(blackImg).convert('L')
# 保证生成的图像够大
whiteWidth, whiteHeight = _im1.size
blackWidth, blackHeight = _im2.size
width = max(whiteWidth, blackWidth)
height = max(whiteHeight, blackHeight)
# 棋盘格化
if chess:
pix = list(_im1.getdata())
for i in range(whiteWidth * whiteHeight):
x = i / whiteWidth
y = i % whiteWidth
if (x + y) % 2 == 0:
pix[i] = 255
_im1.putdata(pix)
pix = list(_im2.getdata())
for i in range(blackWidth * blackHeight):
x = i / blackWidth
y = i % blackWidth
if (x + y) % 2 != 0:
pix[i] = 0
_im2.putdata(pix)
# 建立新的, 大小合适图片
im3 = Image.new('RGBA', (width, height))
im1 = Image.new('L', (width, height), 255)
im2 = Image.new('L', (width, height), 0)
# 将原图复制到新图像里面
im1.paste(_im1, ((width - whiteWidth) // 2, (height - whiteHeight) // 2))
im2.paste(_im2, ((width - blackWidth) // 2, (height - blackHeight) // 2))
# 根据官方文档的说法, getpixel和putpixel效率太低, 换用getdata和putdata
pix1 = im1.getdata()
pix2 = im2.getdata()
pix3 = []
for i in range(width * height):
p1 = pix1[i]
p2 = pix2[i] * light
a = 1 - p1 / 255.0 + p2 / 255.0
r = int(p2 / a if not a == 0 else 255)
pix3.append((r, r, r, int(a * 255)))
im3.putdata(pix3)
return im3
# https://zhuanlan.zhihu.com/p/32532733
def colorfulCar(whiteImg, blackImg, light, m_colorWhite, m_colorBlack):
"""发彩色车"""
_im1 = Image.open(whiteImg).convert('RGB')
_im2 = Image.open(blackImg).convert('RGB')
_im2 = ImageEnhance.Brightness(_im2).enhance(light)
# 将长宽提取提取出来, 提高后面访问的速度
whiteWidth, whiteHeight = _im1.size
blackWidth, blackHeight = _im2.size
width = max(whiteWidth, blackWidth)
height = max(whiteHeight, blackHeight)
# 建立新的, 大小合适图片
im3 = Image.new('RGBA', (width, height))
im1 = Image.new('RGB', (width, height), (255, 255, 255))
im2 = Image.new('RGB', (width, height), (0, 0, 0))
# 将原图复制到新图像里面
im1.paste(_im1, ((width - whiteWidth) // 2, (height - whiteHeight) // 2))
im2.paste(_im2, ((width - blackWidth) // 2, (height - blackHeight) // 2))
# 根据官方文档的说法, getpixel和putpixel效率太低, 换用getdata和putdata
pix1 = im1.getdata()
pix2 = im2.getdata()
pix3 = []
for i in range(width * height):
r1, g1, b1 = [x / 255 for x in pix1[i]]
r2, g2, b2 = [x / 255 for x in pix2[i]]
gray1 = min((r1 * 0.334 + g1 * 0.333 + b1 * 0.333), 1)
r1 = r1 * m_colorWhite + gray1 * (1 - m_colorWhite)
g1 = g1 * m_colorWhite + gray1 * (1 - m_colorWhite)
b1 = b1 * m_colorWhite + gray1 * (1 - m_colorWhite)
# gray1 = min((r1 * 0.334 + g1 * 0.333 + b1 * 0.333), 1)
gray2 = min((r2 * 0.334 + g2 * 0.333 + b2 * 0.333), 1)
r2 = r2 * m_colorBlack + gray2 * (1 - m_colorBlack)
g2 = g2 * m_colorBlack + gray2 * (1 - m_colorBlack)
b2 = b2 * m_colorBlack + gray2 * (1 - m_colorBlack)
# gray2 = min((r2 * 0.334 + g2 * 0.333 + b2 * 0.333), 1)
dr = 1 - r1 + r2
dg = 1 - g1 + g2
db = 1 - b1 + b2
maxc = max(r2, g2, b2)
a = min(max(dr * 0.222 + dg * 0.707 + db * 0.071, maxc), 1)
r = min(r2 / a, 1)
g = min(g2 / a, 1)
b = min(b2 / a, 1)
pix3.append((int(r * 255), int(g * 255), int(b * 255), int(a * 255)))
im3.putdata(pix3)
return im3