|
|
|
|
|
import os |
|
import shutil |
|
import random |
|
|
|
source_dir = './font_images' |
|
organized_dir = './train_test_images' |
|
train_dir = os.path.join(organized_dir, 'train') |
|
test_dir = os.path.join(organized_dir, 'test') |
|
|
|
|
|
os.makedirs(train_dir, exist_ok=True) |
|
os.makedirs(test_dir, exist_ok=True) |
|
|
|
|
|
fonts = [f.split('_')[0] for f in os.listdir(source_dir) if f.endswith('.png')] |
|
fonts = list(set(fonts)) |
|
|
|
for font in fonts: |
|
font_train_dir = os.path.join(train_dir, font) |
|
font_test_dir = os.path.join(test_dir, font) |
|
os.makedirs(font_train_dir, exist_ok=True) |
|
os.makedirs(font_test_dir, exist_ok=True) |
|
|
|
font_files = [f for f in os.listdir(source_dir) if f.startswith(font)] |
|
random.shuffle(font_files) |
|
|
|
train_files = font_files[:int(0.8 * len(font_files))] |
|
test_files = font_files[int(0.8 * len(font_files)):] |
|
|
|
|
|
for train_file in train_files: |
|
shutil.move(os.path.join(source_dir, train_file), font_train_dir) |
|
|
|
|
|
for test_file in test_files: |
|
shutil.move(os.path.join(source_dir, test_file), font_test_dir) |
|
|