This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
IMG_HEIGHT = 512 | |
IMG_WIDTH = 512 | |
paths = filter_fonts_get_paths(df, root=ROOT, subsets=[''], variants=['bold'], category='') | |
r = random.randrange(0, len(paths)) | |
# sample text and font | |
text = "G" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def filter_fonts_get_paths(df, root='./', variants=['_'], subsets=['_'], category=''): | |
# exceptions | |
if not variants or variants == [''] or variants == '': variants = ['_'] | |
if not subsets or subsets == [''] or subsets == '': subsets = ['_'] | |
# apply filters | |
regex_filters = variants + subsets + ['_'+category] | |
df_new = pd.concat([df.filter(regex=re.compile(regex, re.IGNORECASE), axis=1).sum(axis=1).astype(bool) for regex in regex_filters], axis=1) | |
mask = df_new.all(axis=1) | |
filtered_fontnames = list(df.loc[mask].family) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import glob | |
# change the root directory according to your setup | |
ROOT = '../../input/fonts/ofl' | |
# some fonts are within /ofl/fontname/static directory (2 levels deep) | |
all_fonts_path = glob.glob(ROOT + '/**/**/*.ttf', recursive=True) | |
print('number of font files in total: ', len(all_fonts_path)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def filter_fonts(df, regex_filters): | |
df_new = pd.concat([df.filter(regex=re.compile(regex, re.IGNORECASE), axis=1).sum(axis=1).astype(bool) for regex in regex_filters], axis=1) | |
mask = df_new.all(axis=1) | |
return list(df.loc[mask].family) | |
filtered_fontnames = filter_fonts(df, ['Black', 'cyrillic', '_serif']) | |
filtered_fontnames |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
regex_filters = ['_regular', '_japanese', '_serif'] | |
df_new = pd.concat([df.filter(regex=regex, axis=1).sum(axis=1).astype(bool) for regex in regex_filters], axis=1) | |
mask = df_new.all(axis=1) | |
df.loc[mask] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mask = (df.filter(regex='chinese', axis=1).sum(axis=1).astype(bool) & | |
df.filter(regex='sans-serif', axis=1).sum(axis=1).astype(bool)) | |
df_selected = df[mask] | |
df_selected |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mask = (df.filter(regex='thin', axis=1).sum(axis=1).astype(bool) & | |
df.filter(regex='regular', axis=1).sum(axis=1).astype(bool) & | |
df.filter(regex='thai', axis=1).sum(axis=1).astype(bool)) | |
df_selected = df[mask] | |
df_selected |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print('Number of fonts in each variant:') | |
print(df[df.filter(regex=r'^variants_', axis=1).columns].eq(1).sum().sort_values(ascending=False)) | |
print('\nNumber of fonts in each subset:') | |
print(df[df.filter(regex=r'^subsets_', axis=1).columns].eq(1).sum().sort_values(ascending=False)) | |
print('\nNumber of fonts in each category:') | |
print(df[df.filter(regex=r'^category_', axis=1).columns].eq(1).sum().sort_values(ascending=False)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# modules we need this time | |
import matplotlib.pyplot as plt | |
from PIL import Image,ImageDraw,ImageFont | |
import pandas as pd | |
import numpy as np | |
# load the CSV we created in the last post | |
df = pd.read_csv('./google-fonts-annotaion.csv') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
col_names = { | |
"100": "thin", | |
"100italic": "thinitalic", | |
"200": "extralight", | |
"200italic": "extralightitalic", | |
"300": "light", | |
"300italic": "lightitalic", | |
"400": "regular", | |
"regular": "regular", | |
"400italic": "italic", |