21 lines
907 B
Python
21 lines
907 B
Python
# Writes a rolling-hills heightmap (octaves of smooth random noise) as a greyscale PNG.
|
|
# Usage: python3 util/makeHills.py testScripts/hills.png [size]
|
|
import sys
|
|
import numpy as np
|
|
from PIL import Image
|
|
out = sys.argv[1]
|
|
size = int(sys.argv[2]) if len(sys.argv) > 2 else 257
|
|
rng = np.random.default_rng(7)
|
|
def smooth(cells, amplitude):
|
|
grid = rng.random((cells + 1, cells + 1))
|
|
img = Image.fromarray((grid * 255).astype(np.uint8)).resize((size, size), Image.BICUBIC)
|
|
return np.asarray(img).astype(float) / 255.0 * amplitude
|
|
h = smooth(4, 1.0) + smooth(8, 0.5) + smooth(16, 0.25) + smooth(32, 0.12)
|
|
h -= h.min()
|
|
h /= h.max()
|
|
# A flat-ish valley through the middle for things to stand on.
|
|
yy, xx = np.mgrid[0:size, 0:size] / (size - 1)
|
|
valley = np.exp(-((yy - 0.5) ** 2) * 40)
|
|
h = h * (1 - 0.6 * valley) + 0.2 * valley
|
|
Image.fromarray((h * 255).astype(np.uint8)).save(out)
|
|
print(out, size)
|