-
Notifications
You must be signed in to change notification settings - Fork 22
/
1_generateImage.py
42 lines (36 loc) · 1.06 KB
/
1_generateImage.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
# coding: utf-8
import random
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import sys
import os
# how many pictures to generate
num = 10
if len(sys.argv) > 1:
num = int(sys.argv[1])
def genline(text, font, filename):
'''
generate one line
'''
w, h = font.getsize(text)
image = Image.new('RGB', (w + 15, h + 15), 'white')
brush = ImageDraw.Draw(image)
brush.text((8, 5), text, font=font, fill=(0, 0, 0))
image.save(filename + '.jpg')
with open(filename + '.txt', 'w') as f:
f.write(text)
f.close()
if __name__ == '__main__':
if not os.path.isdir('./lines/'):
os.mkdir('./lines/')
for i in range(num):
fontname = './fonts/simkai.ttf'
fontsize = 24
font = ImageFont.truetype(fontname, fontsize)
text = str(random.randint(1000000000, 9999999999))
text = text + str(random.randint(1000000000, 9999999999))
#text = str(random.randint(1000, 9999))
filename = './lines/' + str(i + 1)
genline(text, font, filename)
pass