Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_perlin_noise3.py
- from Tkinter import *
- from PIL import Image, ImageTk
- import random
- import math
- import time
- ww = 600
- hh = 600
- if 1:
- root = Tk()
- root.title("Tk Perlin Noise")
- root.geometry("%dx%d+-10+0"%(ww,hh))
- canvas = Canvas(root, width=ww, height=hh)
- canvas.pack()
- 0
- if 1:
- data = []
- img = Image.new("RGB", (ww, hh))
- octaves = int(math.log(max(ww, hh), 2.0))
- persistence = random.random()
- imgAr = [[0.0 for i in range(ww)] for j in range(hh)] # image array
- totAmp = 0.0
- for k in range(octaves):
- freq = 2 ** k
- amp = persistence ** k
- totAmp += amp
- # create an image from n by m grid of random numbers (w/ amplitude)
- # using Bilinear Interpolation
- n = freq + 1; m = freq + 1 # grid size
- ar = [[random.random() * amp for i in range(n)] for j in range(m)]
- nx = ww / (n - 1.0); ny = hh / (m - 1.0)
- for ky in range(hh):
- for kx in range(ww):
- i = int(kx / nx); j = int(ky / ny)
- dx0 = kx - i * nx; dx1 = nx - dx0
- dy0 = ky - j * ny; dy1 = ny - dy0
- z = ar[j][i] * dx1 * dy1
- z += ar[j][i + 1] * dx0 * dy1
- z += ar[j + 1][i] * dx1 * dy0
- z += ar[j + 1][i + 1] * dx0 * dy0
- z /= nx * ny
- imgAr[ky][kx] += z # add image layers together
- # paint image
- pixels = []
- for ky in range(hh):
- for kx in range(ww):
- c = int(imgAr[ky][kx] / totAmp * 255)
- pixels.append((c, c, c))
- img.putdata(pixels)
- 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