 # Untitled

a guest
May 15th, 2017
145
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #!/usr/bin/env python3
2.
3. import png
4. import numpy as np
5. import random
6.
7. size = 512
8. num_layers = 32
9. density = 1000
10.
12. dmax = 30                # Max horizontal bending
13. n_exp = 2                # Grade of the exponential function that describes the blade
14. min_height = 16          # Minimum height in layers
15.
16. texels = np.zeros((size, size, num_layers), dtype=np.int)
17.
18. for i in range(0, density):
19.     # Fill the base layer. The rest of the layers are computed from it
20.     x = random.randint(0, size-1)
21.     y = random.randint(0, size-1)
22.     texels[y, x, 0] = 1
23.
24.     direction = random.randint(0, 3) # 4 directions
25.     height = random.randint(min_height, num_layers)
26.     factor = random.random();
27.     for layer in range(1, num_layers):
28.         # Exit loop when we reach the blade height
29.         if layer > height:
30.             break
31.
32.         d = ((dmax*pow(layer, n_exp))/pow(num_layers, n_exp))*factor
33.         d = int(d) # To integer
34.
35.         new_x = x;
36.         new_y = y;
37.         if direction == 0:
38.             new_x = x+d
39.         elif direction == 1:
40.             new_x = x-d
41.         elif direction == 2:
42.             new_y = y+d
43.         elif direction == 3:
44.             new_y = y-d
45.
46.         # Discard out of bounds pixels
47.         if new_x >= 0 and new_x < size and new_y >= 0 and new_y < size:
48.             texels[new_y, new_x, layer] = 1
49.
50. # Encode in decimal
51. image = np.zeros((size, size, 4), dtype=np.int)
52. for y in range(0, size):
53.     for x in range(0, size):
54.         curr = np.flipud(texels[y, x])
55.         split_curr = np.split(curr, 4)
56.
57.         for channel in reversed(range(0, 4)):
58.             out = 0
59.             for bit in split_curr[channel]:
60.                 out = (out << 1) | bit
61.
62.             image[y, x, channel] = out
63.
64. png.from_array(image.tolist(), 'RGBA').save("grass_density.png")
RAW Paste Data