-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathGeneratorCharacters.py
More file actions
executable file
·61 lines (50 loc) · 1.45 KB
/
Copy pathGeneratorCharacters.py
File metadata and controls
executable file
·61 lines (50 loc) · 1.45 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
#!/usr/bin/env python
# author: Hua Liang [ Stupid ET ]
# email: et@everet.org
# website: http://EverET.org
#
# This is a tools that generate each image of character
import random, string, md5, time
import Image, ImageDraw, ImageFont
import StringIO, string
def get_vertical_map(img):
img = img.convert('L')
# segmentation
m = [0] * width
for x in range(width):
for y in range(height):
if img.getpixel((x, y)) != 255:
m[x] += 1
return m
def get_character_region(iterator):
'''get character region
'''
state = "empty"
i, lbd, rbd = 0, -1, -1
while True:
data = iterator.next()
if state == "empty":
if data != 0:
# enter
state = "collect"
lbd = i
elif state == "collect":
if data == 0:
# leave
rbd = i
state = "empty"
yield (lbd, rbd)
i += 1
width, height = 1200, 40
characters = string.letters + string.digits
# draw img
img = Image.new("RGBA", (width, height), (255, 255, 255, 0))
font = ImageFont.truetype("../UbuntuMono-R.ttf", 33)
draw = ImageDraw.Draw(img)
draw.setfont(font)
draw.text((10, 0), characters, (0, 0, 0, 255))
del draw
m = get_vertical_map(img)
for index, bd in enumerate(get_character_region(iter(m))):
ch = characters[index]
img.crop((bd[0], 0, bd[1], height)).save('../Images/%s.png' % ch)