-
Notifications
You must be signed in to change notification settings - Fork 17
/
Chinese_inputs.py
52 lines (42 loc) · 1.43 KB
/
Chinese_inputs.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
from PIL import Image,ImageDraw,ImageFont
import random
import numpy as np
# logger = logging.Logger(name='gen verification')
FONTSIZE = 64
class CommonChar():
def __init__(self,fn='common_characters.txt'):
with open(fn,'r',encoding='utf8') as f:
self.chars = [c for c in f.readline().strip()]
class RandomChar():
@staticmethod
def Unicode():
val = random.randint(0x4E00, 0x9FBF)
return chr(val)
class ImageChar():
def __init__(self, fontColor = (0, 0, 0),
size = (FONTSIZE,FONTSIZE),
fontPath = './simsun.ttc',
bgColor = (0, 0, 0),
fontSize = FONTSIZE):
self.size = size
self.fontPath = fontPath
self.bgColor = bgColor
self.fontSize = fontSize
self.fontColor = fontColor
self.font = ImageFont.truetype(self.fontPath, self.fontSize)
self.image = Image.new('RGB', size, bgColor)
def drawText(self, txt, pos=(0,0), fill="#FFFFFF"):
self.image.paste((0,0,0),(0,0,self.size[0],self.size[1]))
draw = ImageDraw.Draw(self.image)
draw.text(pos, txt, font=self.font, fill=fill)
#del draw
def toArray(self):
data = np.array(list(self.image.getdata()))
return data.mean(axis=-1).reshape(self.size)
def save(self, path):
self.image.save(path)
if __name__ == '__main__':
ic = ImageChar()
ic.drawText('矗')
ic.drawText('我')
ic.image.show()