-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-data-creation.py
105 lines (91 loc) · 3.73 KB
/
2-data-creation.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""
data-creation.py :
Créer une multitude d'images à partir des polices disponibles sur la
machine (dans le répertoire "C:\\Windows\\Fonts"), qui seront stockées
dans le répertoire : data/letter-recognition/.
Les polices dans le fichier json 'data/excluded-fonts.json' seront
exclues du traitement.
"""
import argparse
import json
import numpy as np
import os
from PIL import Image, ImageDraw, ImageFont
import shutil as sl
import string
import time
FLAGS = None
SIZE = (20, 20)
LETTER_POS = (3, 0)
EXCLUDED_FONTS_JSON = "data/excluded-fonts.json"
FONT_PATH = "C:\\Windows\\Fonts" # Windows Font Location
EXPORT_DIR = "data/letter-recognition"
def main():
start = time.time()
excluded_fonts = [ ]
if os.path.exists(EXCLUDED_FONTS_JSON):
excluded_fonts = json.loads(open(EXCLUDED_FONTS_JSON).read())
# Création ou suppression du répertoire d'exportation
if os.path.exists(EXPORT_DIR):
sl.rmtree(EXPORT_DIR)
os.makedirs(EXPORT_DIR)
# Création des répertoires d'exportation des lettres
[os.mkdir("{}/{}".format(EXPORT_DIR, letter)) for letter in string.ascii_lowercase]
# + Répertoire du caractère vide
os.mkdir("{}/{}".format(EXPORT_DIR, 'empty'))
# Compte des erreurs et des succès
error = 0
success = 0
printed = 0
# Listing des polices du système
font_files = os.listdir(FONT_PATH)
# Filtrage sur les polices ttf (not fon)
font_files = [font_file for font_file in font_files if not font_file.lower().endswith('.fon')]
# Filtrage des polices exclues
font_files = [font_file for font_file in font_files if not font_file in excluded_fonts]
# Comptage des fichiers de police
count = len(font_files)
for font_file in font_files:
font_path = "{}/{}".format(FONT_PATH, font_file)
n = font_file.find('.')
font_name = font_file[0:n]
try:
# Chargement de la police
font = ImageFont.truetype(font_path, 16)
print("FONT : {}".format(font_name))
success = success + 1
# Création des lettres minuscules
for letter in string.ascii_lowercase:
export_path = "{}/{}/{}_lower.png".format(EXPORT_DIR, letter, font_name)
im = Image.new('RGB', SIZE, (255,255,255))
draw = ImageDraw.Draw(im)
draw.text(LETTER_POS, letter, font=font, fill="black")
im.save(export_path, "PNG")
printed = printed + 1
# Création des lettres majuscules
for letter in string.ascii_uppercase:
export_path = "{}/{}/{}_upper.png".format(EXPORT_DIR, letter, font_name)
im = Image.new('RGB', SIZE, (255,255,255))
draw = ImageDraw.Draw(im)
draw.text(LETTER_POS, letter, font=font, fill="black")
im.save(export_path, "PNG")
printed = printed + 1
# Ajout du caractère vide
im = Image.new('RGB', SIZE, (255,255,255))
export_path = "{}/{}/{}.png".format(EXPORT_DIR, 'empty', font_name)
im.save(export_path, "PNG")
printed = printed + 1
except OSError as ose:
print("Error : {}".format(ose))
error = error + 1
print("Loading Error : {}".format(error))
print("Loading Success : {}".format(success))
print("Font Files : {}".format(count))
print("Saved Images : {}".format(printed))
end = time.time() - start
minutes, seconds = divmod(end, 60)
print("Execution Time : {}m {:.4f}s".format(int(minutes), seconds))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
FLAGS, unparsed = parser.parse_known_args()
main()