-
Notifications
You must be signed in to change notification settings - Fork 0
/
tileset_generator.py
81 lines (66 loc) · 2.73 KB
/
tileset_generator.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
from chessboard_finder import *
import os
import glob
def saveTiles(tiles, img_save_dir, img_file):
letters = 'ABCDEFGH'
if not os.path.exists(img_save_dir):
os.makedirs(img_save_dir)
for i in range(64):
sqr_filename = "%s/%s_%s%d.png" % (img_save_dir, img_file, letters[i%8], i/8+1)
# Make resized 32x32 image from matrix and save
if tiles.shape != (32,32,64):
PIL.Image.fromarray(tiles[:,:,i]) \
.resize([32,32], PIL.Image.ADAPTIVE) \
.save(sqr_filename)
else:
# Possibly saving floats 0-1 needs to change fromarray settings
PIL.Image.fromarray((tiles[:,:,i]*255).astype(np.uint8)) \
.save(sqr_filename)
def generateTileset(input_chessboard_folder, output_tile_folder):
# Create output folder as needed
if not os.path.exists(output_tile_folder):
os.makedirs(output_tile_folder)
# Get all image files of type png/jpg/gif
img_files = set(glob.glob("%s/*.png" % input_chessboard_folder))\
.union(set(glob.glob("%s/*.jpg" % input_chessboard_folder)))\
.union(set(glob.glob("%s/*.gif" % input_chessboard_folder)))
num_success = 0
num_failed = 0
num_skipped = 0
for i, img_path in enumerate(img_files):
print("#% 3d/%d : %s" % (i+1, len(img_files), img_path))
# Strip to just filename
img_file = img_path[len(input_chessboard_folder):-4]
# Create output save directory or skip this image if it exists
img_save_dir = "%s/tiles_%s" % (output_tile_folder, img_file)
if os.path.exists(img_save_dir):
print("\tSkipping existing")
num_skipped += 1
continue
# Load image
print("---")
print("Loading %s..." % img_path)
img_arr = np.array(loadImageGrayscale(img_path), dtype=np.float32)
# Get tiles
print("\tGenerating tiles for %s..." % img_file)
corners = findChessboardCorners(img_arr)
tiles = getChessTilesGray(img_arr, corners)
# Save tiles
if len(tiles) > 0:
print("\tSaving tiles %s" % img_file)
saveTiles(tiles, img_save_dir, img_file)
num_success += 1
else:
print("\tNo Match, skipping")
num_failed += 1
print("\t%d/%d generated, %d failures, %d skipped." % (num_success,
len(img_files) - num_skipped, num_failed, num_skipped))
if __name__ == '__main__':
np.set_printoptions(suppress=True, precision=2)
parser = argparse.ArgumentParser(description='Generate tile images for alll chessboard images in input folder')
parser.add_argument('input_folder', metavar='input_folder', type=str,
help='Input image folder')
parser.add_argument('output_folder', metavar='output_folder', type=str,
help='Output tile folder')
args = parser.parse_args()
generateTileset(args.input_folder, args.output_folder)