forked from breuleux/bugland
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Caglar
committed
May 13, 2012
1 parent
15886d3
commit eca8065
Showing
3 changed files
with
71 additions
and
4 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from __future__ import division | ||
|
||
import Image, ImageDraw | ||
import numpy as np | ||
|
||
#This is a perlin noise generating script | ||
class PerlinNoiseGenerator(object): | ||
def __init__(self, w, h, size=64, rnd=12312): | ||
self.w = w | ||
self.h = h | ||
self.rnd = rnd | ||
self.size = size | ||
self.rng = np.random.RandomState(self.rnd) | ||
|
||
def generate_noise(self): | ||
noise = np.array(np.zeros(self.w*self.h)) | ||
noise = noise.reshape((self.w,self.h)) | ||
for x in xrange(self.w): | ||
for y in xrange(self.h): | ||
noise[x][y] = (self.rng.random_integers(0, 32768) / 32768) | ||
return noise | ||
|
||
def smoothnoise(self, x, y, noise): | ||
x_i = int(x) | ||
y_i = int(y) | ||
fract_x = x - x_i | ||
fract_y = y - y_i | ||
|
||
x1 = (x_i + self.w) % self.w | ||
y1 = (y_i + self.h) % self.h | ||
|
||
#Neighbour Values | ||
x2 = (x1 + self.w - 1) % self.w | ||
y2 = (y1 + self.h - 1) % self.h | ||
|
||
#smooth the noise with bilinear interpolation | ||
val = 0.0 | ||
val += fract_x * fract_y * noise[x1][y1] | ||
val += fract_x * (1 - fract_y) * noise[x1][y2] | ||
val += (1 - fract_x) * fract_y * noise[x2][y1] | ||
val += (1 - fract_x) * (1 - fract_y) * noise[x2][y2] | ||
return val | ||
|
||
def turbulence(self, x, y, noise, size=0): | ||
val = 0.0 | ||
if size == 0: | ||
size = self.size | ||
init_size = size | ||
while(size>=1): | ||
val += self.smoothnoise(x/size, y/size, noise) * size | ||
size /= 2.0 | ||
|
||
return (128 * val/init_size) | ||
|
||
def gen_img(self): | ||
img = Image.new("RGB", (self.w, self.h), "#FFFFFF") | ||
draw = ImageDraw.Draw(img) | ||
noise = self.generate_noise() | ||
for x in xrange(self.w): | ||
for y in xrange(self.h): | ||
r = g = b = int(self.turbulence(x, y, noise)) | ||
draw.point((x, y) , fill=(r, g, b)) | ||
img.save("out.png", "PNG") | ||
|
||
if __name__=="__main__": | ||
perl = PerlinNoiseGenerator(32, 32, size=32) | ||
perl.gen_img() |
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