-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_tiles.py
59 lines (46 loc) · 2.18 KB
/
generate_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
import os
import requests
from PIL import Image
ROOT_URL = "https://raw.githubusercontent.com/civmc-map/tiles/master/terrain/z0/"
print("downloading tiles from map.civmc.tk...")
for i in range(-40, 40 + 1):
for j in range(-40, 40 + 1):
print(f"downloading {i},{j}.png")
r = requests.get(f"{ROOT_URL}{i}%2C{j}.png")
if r.status_code == 404:
img = Image.new("RGB", (256, 256), (0, 0, 0))
img.save(f"{i},{j}.png")
else:
with open(f"{i},{j}.png", "wb") as f:
f.write(r.content)
print("tiles downloaded")
print("combining tiles vertically")
for i in range(-40, 40 + 1):
ims = [f"{i},{j}.png" for j in range(-40, 40 + 1)]
os.system(f"convert {' '.join(ims)} -append {i}.png")
print("combining vertically-combined tiles horizontally")
ims = [f"{i}.png" for i in range(-40, 40 + 1)]
os.system(f"convert {' '.join(ims)} +append combined.png")
print("combined into full image")
_ = input("crop combined.png to 20001x20001 and move to final.png. Press "
"enter when finished")
_ = input("remove all png files except final.png. Press enter when finished")
print("cropping to 400x400 tiles")
os.system('magick final.png -crop 400x400 -set filename:tile '
'"%[fx:page.x/400-25]_%[fx:page.y/400-25]" +repage +adjoin '
'"%[filename:tile].png"')
# dont waste time postprocessing final.png, we're done with it after cropping
print("removing final.png")
os.system("rm final.png")
# FrameRenderer doesn't fill the QImages before painting on them, so it's
# initialized with random data, and leaving transparent pixels allows that to
# show through. Make sure all of our images are totally full of actual pixels.
print("converting transparent pixels to black pixels")
os.system('for file in *.png; do convert ./"$file" -background black -alpha '
'remove -alpha off -set filename:f "%t" "%[filename:f].png"; done')
print("crushing files with pngcrush")
# could add -brute here if we wanted the absolute best compression, but it takes
# 50 times as long and doesn't seem to result in any noticeable compression
# gains.
# on average we cut filesize in half with pngcrush
os.system('for file in *.png; do pngcrush -ow ./"$file"; done')