-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_link.py
executable file
·60 lines (55 loc) · 1.53 KB
/
gen_link.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
#coding:utf-8
from PIL import Image, ImageDraw, ImageFont
import random
import numpy as np
MAX_LETTERS=6
# create a array contain letters and numbers [a b c .....z 0 1 .... 9]
charlist = [chr(i) for i in range(97,123)] + [ str(i) for i in range(10)]
CHAR_SET_LEN=len(charlist) #36
# get random letters
def getletters():
num = random.sample(charlist,MAX_LETTERS)
str=''
value = str.join(num)
return value
# convert text to vector
def text2vec(text):
vector = np.zeros(MAX_LETTERS*CHAR_SET_LEN)
for i in range(MAX_LETTERS):
k = charlist.index(text[i])
vector[k+i*36]=1
return vector
# convert vector to text
def vec2text(vec):
char_pos = vec.nonzero()[0]
if char_pos.shape[0]!=6:
return '######'
text=[]
for i in range(MAX_LETTERS):
char_index = char_pos[i]-36*i
if char_index >36:
return '******'
else:
char = charlist[char_index]
text.append(char)
return "".join(text)
# gray pic
def convert2gray(img):
if len(img.shape) > 2:
gray = np.mean(img, -1)
return gray
else:
return img
# generate pic with text
def gen_link_text_and_image():
image = Image.new('RGB', (68, 24), (0,0,0))
font = ImageFont.truetype('./MONACO.TTF', 19) # the font should be same as your shell
draw = ImageDraw.Draw(image)
text = getletters()
for t in range(len(text)):
draw.text((11 * t + 1, 0), text[t], font=font, fill=(255,255,255))
image=image.resize((160,60))
# image.show()
image=np.array(image)
image=convert2gray(image)
return text2vec(text), image