Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_perlin_noise2.py
- from Tkinter import *
- from PIL import Image, ImageTk
- import random
- import math
- import time
- root = Tk()
- root.title("Tk Perlin Noise")
- ww = 400
- hh = 400
- root.geometry("%dx%d+-10+0"%(ww,hh))
- canvas = Canvas(root, width=ww, height=hh)
- canvas.pack()
- dirs = [(math.cos(a * 2.0 * math.pi / 256),
- math.sin(a * 2.0 * math.pi / 256))
- for a in range(256)]
- def noise(x, y, per):
- def surflet(gridX, gridY):
- distX, distY = abs(x-gridX), abs(y-gridY)
- polyX = 1 - 6*distX**5 + 15*distX**4 - 10*distX**3
- polyY = 1 - 6*distY**5 + 15*distY**4 - 10*distY**3
- hashed = perm[perm[int(gridX)%per] + int(gridY)%per]
- grad = (x-gridX)*dirs[hashed][0] + (y-gridY)*dirs[hashed][1]
- return polyX * polyY * grad
- intX, intY = int(x), int(y)
- return (surflet(intX+0, intY+0) + surflet(intX+1, intY+0) +
- surflet(intX+0, intY+1) + surflet(intX+1, intY+1))
- def fBm(x, y, per, octs):
- val = 0
- for o in range(octs):
- val += 0.5**o * noise(x*2**o, y*2**o, per*2**o)
- return int(val*256)
- freq, octs = 1/32.0, 5
- perm = range(256)
- perm += perm
- while 1:
- data = []
- random.shuffle(perm)
- for y in range(hh):
- for x in range(ww):
- data.append(fBm(x*freq, y*freq, int(ww*hh*freq), octs))
- img = Image.new("L", (ww, hh))
- img.putdata(data)
- imgTk = ImageTk.PhotoImage(img)
- canvas.create_image(0, 0, anchor=NW, image=imgTk)
- canvas.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement