Skip to content

Commit

Permalink
Added the perlin noise script.
Browse files Browse the repository at this point in the history
  • Loading branch information
Caglar committed May 13, 2012
1 parent 15886d3 commit eca8065
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
4 changes: 2 additions & 2 deletions bugland/gen.py
Expand Up @@ -167,8 +167,8 @@ def __iter__(self):
bugs1 = [bug.rotate(ri(0, 3) * 90) for bug in bugs1]
bugs2 = [bug.rotate(ri(0, 3) * 90) for bug in bugs2]
if self.scale:
bugs1 = [bug.scale(ri(1, 2)) for bug in bugs1]
bugs2 = [bug.scale(ri(1, 2)) for bug in bugs2]
bugs1 = [bug.scale(ri(1, 3)) for bug in bugs1]
bugs2 = [bug.scale(ri(1, 3)) for bug in bugs2]
descr = []
for bug in bugs1 + bugs2:
descr.append(((ri(0, self.w - bug.w), ri(0, self.h - bug.h)), bug))
Expand Down
67 changes: 67 additions & 0 deletions bugland/perlin.py
@@ -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()
4 changes: 2 additions & 2 deletions bugland/premade.py
Expand Up @@ -38,8 +38,8 @@ def save_to_file(npy_file_name, n_examples, dataset, e=16):
pentomino16x16 = pentomino(16, 16)
pentomino32x32 = pentomino(32, 32)

pentomino_dir = "/data/lisa/data/pentomino/"
tetromino_dir = "/data/lisa/data/tetromino/"
pentomino_dir = "/home/caglar/Datasets/tetropentomino/"
tetromino_dir = "/home/caglar/Datasets/tetropentomino/"

pentomino16x16_raw = pentomino_dir + "pentomino16x16_raw.npy"
pentomino32x32_raw = pentomino_dir + "pentomino32x32_raw.npy"
Expand Down

0 comments on commit eca8065

Please sign in to comment.