Created
January 13, 2014 20:15
-
-
Save smitec/8407241 to your computer and use it in GitHub Desktop.
Generate tile-able (in 1 direction) mountains. Useful for game backgrounds.
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
import random | |
import Image | |
def fractal_mountain(pixels, damping, seed=0.0): | |
px = [seed, seed] | |
factor = 1.0; | |
while(len(px) < pixels): | |
# loop through and generate | |
new_p = [] | |
for i in range(1, len(px)): | |
r = 2*random.random() - 1; | |
new_p = new_p + [min(abs((px[i] + px[i-1])/2.0 + r*factor),1.0)] | |
factor = factor * damping | |
px_new = [] | |
for i in range(len(px)-1): | |
px_new = px_new + [px[i]] + [new_p[i]] | |
px_new = px_new + [px[-1]] | |
px = px_new | |
return px | |
def generate_mountain(sz, damping, seed): | |
im = Image.new("RGBA", sz, (0, 0, 0, 0)) | |
px = im.load() | |
d = fractal_mountain(sz[0], damping, seed) | |
d = d[:sz[0]] | |
for i in range(sz[0]): | |
r = d[i]*sz[1] | |
for j in range(int(r)): | |
px[i,(sz[1] - 1) - j] = (0,0,0,255) | |
return im | |
if __name__ == "__main__": | |
random.seed() | |
for i in range(3): | |
for j in range(2): | |
im = generate_mountain((512,256), 0.5 - 0.05*i, 0.4) | |
im.save("./output_" + str(i) + "_" + str(j) + ".png") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment