forked from wrenchonline/pyAutomated
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
142 lines (121 loc) · 4.09 KB
/
utils.py
File metadata and controls
142 lines (121 loc) · 4.09 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# -*- coding: utf-8 -*-
import string
from enum import Enum
import cv2
import numpy as np
# RGB格式颜色转换为16进制颜色格式
def toHex(tmp) :
rgb = tmp.split(",")
strs = "#"
for j in range (0, len(rgb)):
num = string.atoi(rgb[j])
strs += str(hex(num))[-2:] #每次转换之后只取0x7b的后两位,拼接到strs中
print("转换后的16进制值为:")
print(strs)
def rgb_to_hex(tup):
"""Convert RGB value to hex."""
return '%02x%02x%02x'.upper() % (tup[0], tup[1], tup[2])
# 16进制颜色格式颜色转换为RGB格式
def Hex_to_RGB(hex):
r = int(hex[2:4],16)
g = int(hex[4:6],16)
b = int(hex[6:8], 16)
# rgb = str(r)+','+str(g)+','+str(b)
# print(rgb)
return r , g ,b
class State(Enum):
OK = 1
ERROR = 2
ROLLBACK = 3
NOTMATCH = 4
ascii_char = list("0@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'.1")
#将256灰度映射到70个字符上
def get_char(r,g,b,alpha=256):#alpha透明度
if alpha==0:
return ' '
length=len(ascii_char)
gray=int(0.2126*r+0.7152*g+0.0722*b)#计算灰度
unit=(256.0+1)/length
return ascii_char[int(gray/unit)]#不同的灰度对应着不同的字符
#rgb误差函数,反正是从pyscreeze这上面抄的魔改的
def pixelMatchesColor(pix,expectedRGBColor,tolerance=0):
"""
TODO
"""
if len(pix)==3 and len(expectedRGBColor) == 3: #RGB mode
r,g,b = pix[:3]
exR, exG, exB = expectedRGBColor[:3]
return (abs(r - exR) <= tolerance) and (abs(g - exG) <= tolerance) and (abs(b - exB) <= tolerance)
else:
assert False, 'Color mode was expected to be length 3 (RGB) or 4 (RGBA), but pixel is length %s and expectedRGBColor is length %s' % (len(pix), len(expectedRGBColor))
def encode(s):
return ' '.join([bin(ord(c)).replace('0b', '') for c in s])
def decode(s):
return ''.join([chr(i) for i in [int(b, 2) for b in s.split(' ')]])
# >>>encode('hello')
# '1101000 1100101 1101100 1101100 1101111'
# >>>decode('1101000 1100101 1101100 1101100 1101111')
# 'hello'
def hexstr_16_to_hexstr_2(hex_str):
x = bin(int('1'+hex_str, 16))[3:]#含有前导0的转换
return x
def hexstr_16_to_hexint_2(hex_str):
x = bytes.fromhex(hex_str)
return x
def display(dianzhen_str,x,y,bx):
dianzhen_str += bx
#初始化22*23的点阵位置,每个汉字需要16*16=256个点来表示
rect_list = list()
for j in range(x):
rect_list.append([] * y)
for k in range(y):
rect_list[j].append([] * y)
i = 0
for j in range(x):
for k in range(y):
rect_list[j][k]=int(dianzhen_str[i])
i+=1;
i = 0
for j in range(y):
for k in range(x):
if rect_list[k][j]:
#前景字符(即用来表示汉字笔画的输出字符)
print('◼', end=' ')
else:
# 背景字符(即用来表示背景的输出字符)
print('◻', end=' ')
print()
def pian_color(img):
l_channel, a_channel, b_channel = cv2.split(img)
h,w,_ = img.shape
da = a_channel.sum()/(h*w)-128
db = b_channel.sum()/(h*w)-128
histA = [0]*256
histB = [0]*256
for i in range(h):
for j in range(w):
ta = a_channel[i][j]
tb = b_channel[i][j]
histA[ta] += 1
histB[tb] += 1
msqA = 0
msqB = 0
for y in range(256):
msqA += float(abs(y-128-da))*histA[y]/(w*h)
msqB += float(abs(y - 128 - db)) * histB[y] / (w * h)
import math
result = math.sqrt(da*da+db*db)/math.sqrt(msqA*msqA+msqB*msqB)
print("d/m = %s"%result)
def binstr_to_nparray(hex_2_str,abs_x,abs_y):
binary = np.zeros((abs_y,abs_x), dtype=np.uint8)
#print("hex_2_str:{0} len:{1}".format(hex_2_str,len(hex_2_str)))
#display(hex_2_str,abs_x,abs_y,"")
i = 0
for j in range(abs_x):
for k in range(abs_y):
if hex_2_str[i] == "0":
binary[k][j]=0
else:
binary[k][j]=255
i+=1
return binary