Skip to content

Instantly share code, notes, and snippets.

@Don-Moahskarton
Created May 29, 2020 17:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Don-Moahskarton/287abd17b7ad33a7dada45429c3a93ae to your computer and use it in GitHub Desktop.
Save Don-Moahskarton/287abd17b7ad33a7dada45429c3a93ae to your computer and use it in GitHub Desktop.
import cv2
import sys
import numpy as np
#config
outputSize = 1017
image = cv2.imread('in.png', cv2.IMREAD_UNCHANGED)
dimensions = image.shape
# height, width, number of channels in image
height = image.shape[0]
width = image.shape[1]
print('heightmap Height : ',height)
print('heightmap Width : ',width)
#imS = cv2.resize(image, (512, 512))
#cv2.imshow("original (512 resize)", imS)
x_name = 0
x = 0
while x+outputSize < width:
y = 0
y_name = 0
while y+outputSize < height:
out_file_name = "IMG_X" + str(x_name) + "_Y" + str(y_name) + ".png"
print("Currently rendering " + out_file_name)
crop_image = image[y:y+outputSize, x:x+outputSize]
cv2.imwrite(out_file_name, crop_image.astype(np.uint16))
y+=outputSize-1
y_name+=1
x+=outputSize-1
x_name+=1
cv2.waitKey(0)
@akiszelewski
Copy link

Hello, thank you for the tutorial on ue4 forum. I am super new and not sure where to run this code? Can you give me a few more specific instructions to get this up and running? Thanks

@JonasDaWi
Copy link

Great! Thanks for sharing!

@Don-Moahskarton
Copy link
Author

Hello, thank you for the tutorial on ue4 forum. I am super new and not sure where to run this code? Can you give me a few more specific instructions to get this up and running? Thanks

Well you just need python 3.6, numpy and opencv4.
Run this where you want, as standalone python script. It expects a file called "in.png" in the current folder and will output in the same folder the tiled heightmap (IMG_Xx_Yy.png), all of it in 16bits.

@cyber-speed
Copy link

This is a great find, but i have another question, is there a way to use same script but in reverse?
Meaning i have the tiles in RAW format of 32-bit floating points, and I'd like to merge them back to a single height map and then to slice them for UE using script above since i no longer have the original large single height map.
Is there a way you could help at all?
The tiles i have are 257x257 and must overlap by 1 pixel.
The end result will not be a power of 2 height map.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment