-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathnoise.py
More file actions
executable file
·145 lines (114 loc) · 4.43 KB
/
noise.py
File metadata and controls
executable file
·145 lines (114 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python
from PIL import Image
import random, math
"""
Texture generation using Perlin noise
"""
class NoiseUtils:
def __init__(self, imageSize):
self.imageSize = imageSize
self.gradientNumber = 256
self.grid = [[]]
self.gradients = []
self.permutations = []
self.img = {}
self.__generateGradientVectors()
self.__normalizeGradientVectors()
self.__generatePermutationsTable()
def __generateGradientVectors(self):
for i in range(self.gradientNumber):
while True:
x, y = random.uniform(-1, 1), random.uniform(-1, 1)
if x * x + y * y < 1:
self.gradients.append([x, y])
break
def __normalizeGradientVectors(self):
for i in range(self.gradientNumber):
x, y = self.gradients[i][0], self.gradients[i][1]
length = math.sqrt(x * x + y * y)
self.gradients[i] = [x / length, y / length]
# The modern version of the Fisher-Yates shuffle
def __generatePermutationsTable(self):
self.permutations = [i for i in range(self.gradientNumber)]
for i in reversed(range(self.gradientNumber)):
j = random.randint(0, i)
self.permutations[i], self.permutations[j] = \
self.permutations[j], self.permutations[i]
def getGradientIndex(self, x, y):
return self.permutations[(x + self.permutations[y % self.gradientNumber]) % self.gradientNumber]
def perlinNoise(self, x, y):
qx0 = int(math.floor(x))
qx1 = qx0 + 1
qy0 = int(math.floor(y))
qy1 = qy0 + 1
q00 = self.getGradientIndex(qx0, qy0)
q01 = self.getGradientIndex(qx1, qy0)
q10 = self.getGradientIndex(qx0, qy1)
q11 = self.getGradientIndex(qx1, qy1)
tx0 = x - math.floor(x)
tx1 = tx0 - 1
ty0 = y - math.floor(y)
ty1 = ty0 - 1
v00 = self.gradients[q00][0] * tx0 + self.gradients[q00][1] * ty0
v01 = self.gradients[q01][0] * tx1 + self.gradients[q01][1] * ty0
v10 = self.gradients[q10][0] * tx0 + self.gradients[q10][1] * ty1
v11 = self.gradients[q11][0] * tx1 + self.gradients[q11][1] * ty1
wx = tx0 * tx0 * (3 - 2 * tx0)
v0 = v00 + wx * (v01 - v00)
v1 = v10 + wx * (v11 - v10)
wy = ty0 * ty0 * (3 - 2 * ty0)
return (v0 + wy * (v1 - v0)) * 0.5 + 1
def makeTexture(self, texture = None):
if texture is None:
texture = self.cloud
noise = {}
max = min = None
for i in range(self.imageSize):
for j in range(self.imageSize):
value = texture(i, j)
noise[i, j] = value
if max is None or max < value:
max = value
if min is None or min > value:
min = value
for i in range(self.imageSize):
for j in range(self.imageSize):
self.img[i, j] = (int) ((noise[i, j] - min) / (max - min) * 255 )
def fractalBrownianMotion(self, x, y, func):
octaves = 12
amplitude = 1.0
frequency = 1.0 / self.imageSize
persistence = 0.5
value = 0.0
for k in range(octaves):
value += func(x * frequency, y * frequency) * amplitude
frequency *= 2
amplitude *= persistence
return value
def cloud(self, x, y, func = None):
if func is None:
func = self.perlinNoise
return self.fractalBrownianMotion(8 * x, 8 * y, func)
def wood(self, x, y, noise = None):
if noise is None:
noise = self.perlinNoise
frequency = 1.0 / self.imageSize
n = noise(4 * x * frequency, 4 * y * frequency) * 10
return n - int(n)
def marble(self, x, y, noise = None):
if noise is None:
noise = self.perlinNoise
frequency = 1.0 / self.imageSize
n = self.fractalBrownianMotion(8 * x, 8 * y, self.perlinNoise)
return (math.sin(16 * x * frequency + 4 * (n - 0.5)) + 1) * 0.5
if __name__ == "__main__":
imageSize = 512
noise = NoiseUtils(imageSize)
noise.makeTexture(texture = noise.cloud)
img = Image.new("L", (imageSize, imageSize))
pixels = img.load()
for i in range(0, imageSize):
for j in range(0, imageSize):
c = noise.img[i, j]
pixels[i, j] = c
img.save("temp.png")