ed.brz9.dev/proj/map/mapcutter.py

57 lines
1.2 KiB
Python

import os
import cairosvg
from PIL import Image
Image.MAX_IMAGE_PIXELS = None
zmax = 6
tileSize = 256
tmpPath = "asset/tmp/"
tilePath = "asset/tiles/"
zMapPathSVG = "asset/svg/zmap/"
output_width=None,
output_height=None
useSVG = False
useSVG = True
def checkPath(path):
if os.path.exists(path):
return
else:
os.makedirs(path)
def svg2png(svg,png,size):
cairosvg.svg2png(
url = svgMap,
output_width = size,
output_height = size,
write_to = zMapPath)
for z in range(zmax + 1):
print("Starting to work on zoomlevel: " + str(z))
size = pow(2,z) * tileSize
zMapPath = tmpPath + str(z) + ".png"
if useSVG:
svgMap = zMapPathSVG + str(z) + ".svg"
svg2png(svgMap,zMapPath,size)
img = Image.open(zMapPath)
top = pow(2,z)
step = tileSize
for x in range(top):
x0 = x*step
x1 = x0 + step
for y in range(top):
y0 = y*step
y1 = y0 + step
area = (x0,y0,x1,y1)
crop = img.crop(area)
path = tilePath + str(z) + "/" + str(x)
checkPath(path)
fn = path + "/" + str(y) + ".png"
tile = crop.resize((tileSize, tileSize))
tile.save(fn,"PNG")