Last active
May 3, 2019 16:01
-
-
Save MathijsdeBoer/a29172fb6f1b6654908cfc7ea5e09a1e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Converts a single heightmap to a tiled heightmap | |
Usage: | |
in a CLI/powershell: | |
> python convert_l3dt_to_ue4.py '<path to input hm>' '<path to output directory' <output size> | |
Example: | |
> python convert_l3dt_to_ue4.py 'c:/l3dt/hm.png' 'c:/l3dt/output' 512 | |
''' | |
import imageio | |
import numpy | |
import os | |
import sys | |
# Disable maximum pixel size, we do not accept images from other users, so we can disable the | |
# attack vector protection | |
from PIL import Image | |
Image.MAX_IMAGE_PIXELS = None | |
# Sort input | |
input_file = sys.argv[1] | |
output_dir = sys.argv[2] | |
tile_size = int(sys.argv[3]) | |
print("Converting large HM to mosaic...") | |
print("Input\t\t-\t{}".format(input_file)) | |
print("Output\t\t-\t{}".format(output_dir)) | |
print("Tile size\t-\t{}\n".format(tile_size)) | |
# Read | |
image = imageio.imread(input_file) | |
print("Loaded image with size ({}, {})".format(image.shape[0], image.shape[1])) | |
if not os.path.exists(output_dir): | |
os.makedirs(output_dir) | |
# Generate tile origins | |
y_tile = 0 | |
for y in range(0, image.shape[1], tile_size-1): | |
x_tile = 0 | |
for x in range(0, image.shape[0], tile_size-1): | |
if image.shape[0] - x > tile_size - 1 and image.shape[1] - y > tile_size - 1: | |
print(x_tile, y_tile, end="\r") | |
filename = "output_X{}_Y{}.png".format(y_tile, x_tile) | |
imageio.imwrite(os.path.join(output_dir, filename), image[x:x+tile_size-1, y:y+tile_size-1]) | |
x_tile += 1 | |
y_tile += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment