Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_rainbow_perlin_noise_v2.py
- from tkinter import *
- from PIL import Image, ImageTk
- import random
- import math
- ww = 500
- hh = 500
- root = Tk()
- root.title("Rainbow Perlin Noise")
- root.geometry("%dx%d+0+0"%(ww,hh))
- canvas = Canvas(root, width=ww, height=hh)
- canvas.grid()
- root.update()
- # create new image
- img = Image.new("RGB",(ww, hh), "white")
- 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):
- # Perlin noise is generated from a summation of little "surflets" which are the product of a randomly oriented
- # gradient and a separable polynomial falloff function.
- def surflet(gridX, gridY):
- distX, distY = abs(x-gridX), abs(y-gridY)
- polyX = 1 - 6*distX**5 + 15*distX**4 - 10*distX**3 # polynomial falloff function
- 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 val
- rainbow=[]
- def z(r,g,b):
- rainbow.append((r,g,b))
- r,g,b=255,0,0
- for g in range(256):
- z(r,g,b)
- for r in range(254, -1, -1):
- z(r,g,b)
- for b in range(256):
- z(r,g,b)
- for g in range(254, -1, -1):
- z(r,g,b)
- for r in range(256):
- z(r,g,b)
- for b in range(254, -1, -1):
- z(r,g,b)
- rainbow*=2
- colors = len(rainbow)/2-1
- while 1:
- perm = list(range(256))
- random.shuffle(perm)
- perm += perm
- xyz = []
- size, freq, octs = ww*hh, 1/132.0, 5 # <<<
- frame = int(size*freq)
- for y in range(hh):
- for x in range(ww):
- zzz = fBm(x*freq, y*freq, frame, octs)
- xyz.append(rainbow[int(zzz*colors+colors)])
- img.putdata(xyz)
- imgTk = ImageTk.PhotoImage(img)
- #time.sleep(0.02)
- canvas.create_image(-2, 0, anchor=NW, image=imgTk)
- root.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement