-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapswipe_display_grid_random_tiles.py
executable file
·91 lines (62 loc) · 2.34 KB
/
mapswipe_display_grid_random_tiles.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
#!/usr/local/bin/python3
# mapswipe_display_grid_random_tiles.py
# Copyright 2017 Robert Jones [email protected]
# Project repo: https://github.com/craic/mapswipe_utils
# Released under the terms of the MIT License
# Simple script that generates a grid image made up of x * y image tiles selected at random
# Handy for getting a quick overview of a directory of image tiles
import argparse
import sys
import os
import json
import random
from PIL import Image
def main():
parser = argparse.ArgumentParser(description="Display a grid of image tiles")
parser.add_argument('--tilelist', '-f', metavar='<tile_list_file>', required=True,
help='MapSwipe Project Tile List file')
parser.add_argument('--tiledir', '-d', metavar='<tile_directory>', required=True,
help='Directory of tile images')
parser.add_argument('--nx', '-x', metavar='<nx>', type=int, required=True,
help='Number of tiles in X dimension')
parser.add_argument('--ny', '-y', metavar='<ny>', type=int, required=True,
help='Number of tiles in Y dimension')
args = parser.parse_args()
nx = int(args.nx)
ny = int(args.ny)
tile_list_file = args.tilelist
tile_dir = args.tiledir
# create the base image
# Bing maps tiles are 256 x 256 pixels
tile_size = 256
image_width = nx * (tile_size + 1) + 1
image_height = ny * (tile_size + 1) + 1
base_image = Image.new('RGBA', (image_width, image_height), (0,0,0))
selected_tiles = {}
# Load the tile_ids into a list
with open(tile_list_file, 'rt') as f:
tile_ids = f.read().splitlines()
used_ints = {}
n_tiles = len(tile_ids)
print(n_tiles)
x = 0
y = 1
for i in range(ny):
x = 1
for j in range(nx):
k = random.randint(0, n_tiles-1)
# keep looking if this tile has already been seen
while k in used_ints:
k = random.randint(0, n_tiles-1)
used_ints[k] = 1
tile_id = tile_ids[k]
print(tile_id)
img_path = os.path.join(tile_dir, "{}.jpg".format(tile_id))
img = Image.open(img_path)
img = img.resize((tile_size, tile_size), Image.ANTIALIAS)
base_image.paste(img, (x, y))
x += tile_size + 1
print('')
y += tile_size + 1
base_image.show()
main()