Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_colorful_perlin_noise.py
- from tkinter import *
- from PIL import Image, ImageTk
- import random
- import math
- wt = 256
- ht = 256
- root = Tk()
- root.title("Colorful Perlin Noise")
- root.geometry("%dx%d+0+0"%(wt,ht))
- canvas = Canvas(root, width=wt, height=ht)
- canvas.grid()
- # create new image
- img = Image.new("RGB",(wt, ht), "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
- xy = []
- size, freq, octs = 256, 1/32.0, 5
- for y in range(size):
- for x in range(size):
- zzz = fBm(x*freq, y*freq, int(size*freq), octs)
- xy.append(rainbow[int(zzz*colors+colors)])
- img.putdata(xy)
- 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