-
Notifications
You must be signed in to change notification settings - Fork 0
/
genData.py
54 lines (47 loc) · 1.57 KB
/
genData.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
import Image, ImageDraw, ImageFont
import random, os, shutil
folder = 'data'
concrete = True
if os.path.exists(folder):
shutil.rmtree(folder)
if(concrete):
fonts = ['comicbd.ttf']#, 'arialbd.ttf'] #, 'arialbd.ttf', 'ariblk.ttf']
size_mu = 16
size_sig = 2
pos_sig = 2
color_sig = 10
rotate_sig = 0
else:
fonts = ['arial.ttf', 'ariali.ttf', 'comicbd.ttf']#, 'HARNGTON.TTF', 'FREESCPT.TTF'] #'LHANDW.TTF', 'HoboStd.otf']
size_mu = 19
size_sig = 3
pos_sig = 0
color_sig = 100
rotate_sig = 25
color_bg = 'white'
nums = xrange(1,10)
samples = xrange(0,500)
width, height = 20, 20
def distr(a, b):
return a+random.randint(-b, b)
def rotate(image, angle, color):
bg = Image.new("RGB", image.size, color)
im = image.convert("RGBA").rotate(angle)
bg.paste(im, im)
return bg
for num in nums:
dir = os.path.dirname(folder + '/' + str(num) + '/')
if not os.path.exists(dir):
os.makedirs(dir)
for i in samples:
font = ImageFont.truetype(random.choice(fonts), distr(size_mu, size_sig))
text_width, text_height = font.getsize(str(num))
#print text_width, text_height
img = Image.new('RGB', (width, height), color_bg)
draw = ImageDraw.Draw(img)
pos = (width/2-text_width/2, height/2-text_height/3)
pos = pos[0]+distr(0, pos_sig), pos[1]+distr(0, pos_sig)
v = random.randint(0, color_sig)
draw.text(pos, str(num), font=font, fill=(v,v,v))
img = rotate(img, distr(0, rotate_sig), color_bg)
img.save(os.path.join(dir, str(i)+'.png'))