Refactoring; Renamed to constants

This commit is contained in:
theAlexGuy
2020-02-11 02:49:20 +01:00
parent cd54af0f16
commit 88f48afd07
5 changed files with 85 additions and 111 deletions

2
.idea/misc.xml generated
View File

@@ -3,5 +3,5 @@
<component name="JavaScriptSettings"> <component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" /> <option name="languageLevel" value="ES6" />
</component> </component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.5.2 (/usr/bin/python3.5)" project-jdk-type="Python SDK" /> <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (2)" project-jdk-type="Python SDK" />
</project> </project>

View File

@@ -2,12 +2,10 @@
<module type="PYTHON_MODULE" version="4"> <module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager"> <component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/data" /> <excludeFolder url="file://$MODULE_DIR$/data" />
</content> </content>
<orderEntry type="jdk" jdkName="Python 3.5.2 (/usr/bin/python3.5)" jdkType="Python SDK" /> <orderEntry type="jdk" jdkName="Python 3.8 (2)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
<component name="TestRunnerService">
<option name="PROJECT_TEST_RUNNER" value="Unittests" />
</component>
</module> </module>

Binary file not shown.

View File

@@ -1,21 +1,3 @@
#from IPython.core.completer import back_latex_name_matches
#from PIL import Image, ImageDraw, ImageFont
# get an image
#base = Image.open('Pillow/Tests/images/hopper.png').convert('RGBA')
# make a blank image for the text, initialized to transparent text color
#img = Image.new('RGB', (30, 30), (255,255,255))
#
# # get a font
# fnt = ImageFont.truetype('resources\HelveticaNeueLight.ttf', 40)
# # get a drawing context
# d = ImageDraw.Draw(img)
#
#
# # draw text
# d.text((0,0), "T", font=fnt, fill=(0,0,0))
# text_width, text_height = d.textsize('T')
# print("width {:d} - height {:d}".format(text_width, text_height))
# ------------------------------ Generate Characters ----------------------------# # ------------------------------ Generate Characters ----------------------------#
# Import python imaging libs # Import python imaging libs
@@ -27,13 +9,72 @@ from PIL import ImageFont, ImageFilter
import numpy as np import numpy as np
# Import operating system lib # Import operating system lib
import os, errno import os, errno
from ImageHelper import noisy
# Import random generator # Import random generator
from random import randint from random import randint
import shutil import shutil
import csv import csv
import pickle import pickle
#---------------------------------- Input and Output ---------------------------#
# Directory containing fonts
font_dir = '../resources/fonts'
# Output
out_dir = '../results/'
k = 0
# ------------------------------------ Characters -------------------------------#
# Numbers
NUMBERS = ['0', '1', '2']
# Small letters
SMALL_LETTERS = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
SMALL_LETTERS_SAMPLE = ['k', 'b', 'c', 'e']
CAPITAL_LETTERS = [x.capitalize() for x in SMALL_LETTERS]
CAPITAL_LETTERS_SAMPLE = [x.capitalize() for x in SMALL_LETTERS_SAMPLE]
# Capital letters
#capital_letters = ["A", 'B', 'C']
# Select characters
CHARACTERS = SMALL_LETTERS
CHARACTERS_SAMPLE = SMALL_LETTERS_SAMPLE
#------------------------------------- Colors ----------------------------------#
# Background color
COLORS_WHITE = [(255, 255, 255)]
COLORS_WHITE_SAMPLE = COLORS_WHITE
COLORS_BLACK = (0, 10, 20, 30)
COLORS_GRAY = [(135, 145, 155)]
#background_colors = white_colors
COLORS_BACKGRND_CHOSEN = COLORS_WHITE_SAMPLE
# -------------------------------------- Sizes ----------------------------------#
# Character sizes
FONTSIZE_SMALL = (10, 14, 16)
FONTSIZE_MEDIUM = (20, 24, 28)
FONTSIZE_LARGE = (30, 36, 40)
FONTSIZE_LARGE_SAMPLE = (24, 30)
FONTSIZE_CHOSEN = FONTSIZE_LARGE_SAMPLE
# Image size
IMAGE_SIZE = 32
OFFSET_X = [-0.3, -0.1, 0, 0.1, 0.3]
OFFSET_Y = [-0.3, -0.1, 0, 0.1, 0.3]
OFFSET_X_SAMPLE = [-0.1, 0, 0.1]
OFFSET_Y_SAMPLE = [-0.3, -0.1, 0, 0.1, 0.3]
# -------------------------------------- rotation and offset ----------------------------------#
ROTATION = [-0.1, -0.5, 0, 0.5, 0.1]
ROTATION_SAMPLE = [0]
# ------------------------------------ Cleanup ----------------------------------# # ------------------------------------ Cleanup ----------------------------------#
@@ -53,10 +94,10 @@ def cleanup():
def generateCharacters(mode ='image', sample = True): def generateCharacters(mode ='image', sample = True):
if(mode == 'image'): if(mode == 'image'):
myoffsetsX = offsetsX_sample if sample else offsetsX myoffsetsX = OFFSET_X_SAMPLE if sample else OFFSET_X
myoffsetsY = offsetsY_sample if sample else offsetsY myoffsetsY = OFFSET_Y_SAMPLE if sample else OFFSET_Y
mydegrees = degrees_sample if sample else degrees mydegrees = ROTATION_SAMPLE if sample else ROTATION
mycharacters = characters_sample if sample else characters mycharacters = CHARACTERS_SAMPLE if sample else CHARACTERS
cleanup() cleanup()
# Counter # Counter
k = 1 k = 1
@@ -69,10 +110,10 @@ def generateCharacters(mode ='image', sample = True):
# For each character do # For each character do
for char in mycharacters: for char in mycharacters:
# For each font size do # For each font size do
for font_size in font_sizes: for font_size in FONTSIZE_CHOSEN:
if font_size > 0: if font_size > 0:
# For each background color do # For each background color do
for background_color in background_colors: for background_color in COLORS_BACKGRND_CHOSEN:
for offsetx in myoffsetsX: for offsetx in myoffsetsX:
for offsety in myoffsetsY: for offsety in myoffsetsY:
for degree in mydegrees: for degree in mydegrees:
@@ -81,7 +122,7 @@ def generateCharacters(mode ='image', sample = True):
"font size: {}\t" "font size: {}\t"
"degree: {}\t \t" "degree: {}\t \t"
"offsetX: {}\t" "offsetX: {}\t"
"offsetY: {}".format(image_size, char, font_size, degree,offsetx, offsety)) "offsetY: {}".format(IMAGE_SIZE, char, font_size, degree, offsetx, offsety))
try: try:
dir = out_dir+char+'/' dir = out_dir+char+'/'
@@ -98,25 +139,25 @@ def generateCharacters(mode ='image', sample = True):
GenerateCSVData(sample) GenerateCSVData(sample)
def GenerateCSVData(sample = True): def GenerateCSVData(sample = True):
myoffsetsX = offsetsX_sample if sample else offsetsX myoffsetsX = OFFSET_X_SAMPLE if sample else OFFSET_X
myoffsetsY = offsetsY_sample if sample else offsetsY myoffsetsY = OFFSET_Y_SAMPLE if sample else OFFSET_Y
mydegrees = degrees_sample if sample else degrees mydegrees = ROTATION_SAMPLE if sample else ROTATION
mycharacters = characters_sample if sample else characters mycharacters = CHARACTERS_SAMPLE if sample else CHARACTERS
print("image size: {} \t " print("image size: {} \t "
"characters: {}\t" "characters: {}\t"
"backgrounds: {}\t" "backgrounds: {}\t"
"font sizes: {}\t" "font sizes: {}\t"
"degree: {}\t" "degree: {}\t"
"offsetX: {}\t" "offsetX: {}\t"
"offsetY: {}".format(image_size, mycharacters, background_colors, font_sizes, mydegrees,myoffsetsX, myoffsetsY)) "offsetY: {}".format(IMAGE_SIZE, mycharacters, COLORS_BACKGRND_CHOSEN, FONTSIZE_CHOSEN, mydegrees, myoffsetsX, myoffsetsY))
k = 0 k = 0
i = 0 i = 0
path, dirs, files = next(os.walk(font_dir)) path, dirs, files = next(os.walk(font_dir))
file_count = len(files) file_count = len(files)
height = file_count*len(mycharacters)*len(font_sizes)*len(background_colors)*len(myoffsetsX)*len(myoffsetsY)*len(mydegrees)*3 height = file_count * len(mycharacters) * len(FONTSIZE_CHOSEN) * len(COLORS_BACKGRND_CHOSEN) * len(myoffsetsX) * len(myoffsetsY) * len(mydegrees) * 3
print(height) print(height)
width = image_size*image_size+1 width = IMAGE_SIZE * IMAGE_SIZE + 1
output = np.ones((height,width), dtype=object) output = np.ones((height,width), dtype=object)
mapDic = {} mapDic = {}
for dirname, dirnames, filenames in os.walk(font_dir): for dirname, dirnames, filenames in os.walk(font_dir):
@@ -129,10 +170,10 @@ def GenerateCSVData(sample = True):
for char in mycharacters: for char in mycharacters:
mapDic[char] = i mapDic[char] = i
# For each font size do # For each font size do
for font_size in font_sizes: for font_size in FONTSIZE_CHOSEN:
if font_size > 0: if font_size > 0:
# For each background color do # For each background color do
for background_color in background_colors: for background_color in COLORS_BACKGRND_CHOSEN:
for offsetx in myoffsetsX: for offsetx in myoffsetsX:
for offsety in myoffsetsY: for offsety in myoffsetsY:
for degree in mydegrees: for degree in mydegrees:
@@ -195,7 +236,7 @@ def getMapDic():
def createImageTemplate(background_color, char, font_size, k, font_filename, degree = 0, offsetX = 0, offsetY = 0, noise = 0): def createImageTemplate(background_color, char, font_size, k, font_filename, degree = 0, offsetX = 0, offsetY = 0, noise = 0):
# Create character image : # Create character image :
# Grayscale, image size, background color # Grayscale, image size, background color
char_image = Image.new('RGBA', (image_size, image_size), \ char_image = Image.new('RGBA', (IMAGE_SIZE, IMAGE_SIZE), \
background_color) background_color)
# Draw character image # Draw character image
draw = ImageDraw.Draw(char_image) draw = ImageDraw.Draw(char_image)
@@ -205,9 +246,9 @@ def createImageTemplate(background_color, char, font_size, k, font_filename, deg
# Get character width and height # Get character width and height
(font_width, font_height) = font.getsize(char) (font_width, font_height) = font.getsize(char)
# Calculate x position # Calculate x position
x = (image_size - font_width) / 2 + offsetX*image_size x = (IMAGE_SIZE - font_width) / 2 + offsetX * IMAGE_SIZE
# Calculate y position # Calculate y position
y = (image_size - font_height) / 2 + offsetY*image_size y = (IMAGE_SIZE - font_height) / 2 + offsetY * IMAGE_SIZE
# Draw text : Position, String, # Draw text : Position, String,
# Options = Fill color, Font # Options = Fill color, Font
draw.text((x, y), char,(0,0,0), font=font) draw.text((x, y), char,(0,0,0), font=font)
@@ -216,11 +257,6 @@ def createImageTemplate(background_color, char, font_size, k, font_filename, deg
# Final file name # Final file name
file_name = "{}_f_{}_fs_{}_bc_{}_n_{}_r_{}_ox_{}_oy_{}_{}.png".\ file_name = "{}_f_{}_fs_{}_bc_{}_n_{}_r_{}_ox_{}_oy_{}_{}.png".\
format(char,fontname, font_size,background_color,noise,degree,offsetX,offsetY,k) format(char,fontname, font_size,background_color,noise,degree,offsetX,offsetY,k)
#str(font_size) + '_bc_' + \
# str(background_color) + '_r_' + \
# str(background_color) + '_r_' + \
# str(background_color) + '_r_' + \
# str(k) + '.png'
# Save image # Save image
#char_image.save('results/' + file_name) #char_image.save('results/' + file_name)
@@ -229,7 +265,7 @@ def createImageTemplate(background_color, char, font_size, k, font_filename, deg
def createImage(directory, background_color, char, font_size, k, font_filename, degree = 0, offsetX = 0, offsetY = 0, noise = 0, save = True): def createImage(directory, background_color, char, font_size, k, font_filename, degree = 0, offsetX = 0, offsetY = 0, noise = 0, save = True):
blankImage, file_name = createBlankImage() blankImage, file_name = createBlankImage()
blankImage.save('blankIamge.png') #blankImage.save('blankImage.png')
image2, file_name2 = createImageTemplate(background_color, char, font_size, k, font_filename, degree, offsetX, offsetY, noise) image2, file_name2 = createImageTemplate(background_color, char, font_size, k, font_filename, degree, offsetX, offsetY, noise)
#image2.save('./results/'+str(k)+'.png') #image2.save('./results/'+str(k)+'.png')
px, py = 0, 0 px, py = 0, 0
@@ -263,79 +299,19 @@ def addNoise(image, noise = 0):
def createBlankImage(): def createBlankImage():
# Create character image : # Create character image :
# Grayscale, image size, background color # Grayscale, image size, background color
char_image = Image.new('RGBA', (image_size, image_size), \ char_image = Image.new('RGBA', (IMAGE_SIZE, IMAGE_SIZE), \
(255,255,255)) (255,255,255))
# w = char_image.rotate(45.5, expand=1) # w = char_image.rotate(45.5, expand=1)
#char_image.rotate(45).show() #char_image.rotate(45).show()
# Final file name # Final file name
file_name = 'blankoImage {}x{}.png'.format(image_size, image_size) file_name = 'blankoImage {}x{}.png'.format(IMAGE_SIZE, IMAGE_SIZE)
#print(file_name) #print(file_name)
# Save image # Save image
#char_image.save('results/' + file_name) #char_image.save('results/' + file_name)
return (char_image, file_name) return (char_image, file_name)
#---------------------------------- Input and Output ---------------------------#
# Directory containing fonts
font_dir = './resources/fonts'
# Output
out_dir = './results/'
k = 0
# ------------------------------------ Characters -------------------------------#
# Numbers
numbers = ['0', '1', '2']
# Small letters
small_letters = ['a', 'b', 'c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
small_letters_limited = ['k', 'b', 'c','e']
capital_letters = [x.capitalize() for x in small_letters_limited]
#print(big_letters)
# Capital letters
#capital_letters = ["A", 'B', 'C']
# Select characters
characters = small_letters
characters_sample = small_letters_limited
#------------------------------------- Colors ----------------------------------#
# Background color
white_colors = [(255,255,255),(220,220,220)]
white_colors_sample = [(255,255,255)]
black_colors = (0, 10, 20, 30)
gray_colors = [(135, 145, 155)]
#background_colors = white_colors
background_colors = white_colors_sample
# -------------------------------------- Sizes ----------------------------------#
# Character sizes
small_sizes = (10, 14, 16)
medium_sizes = (20, 24, 28)
medium_sizes = (20, 24, 28)
large_sizes = (30,36,40)
large_sizes_sample = (24,30)
font_sizes = large_sizes_sample
# Image size
image_size = 32
offsetsX = [-0.3,-0.1,0,0.1,0.3]
offsetsY = [-0.3,-0.1,0,0.1,0.3]
degrees = [-0.1,-0.5,0,0.5,0.1]
offsetsX_sample = [-0.1,0,0.1]
offsetsY_sample = [-0.3,-0.1,0,0.1,0.3]
degrees_sample = [-0.1,-0.5,0,0.5,0.1]
# -------------------------------------- rotation and offset ----------------------------------#