Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_perlin_noise.py -- very slow
- dir=r"C:/pydemo/test/"
- from Tkinter import *
- import random
- import math
- from PIL import Image
- from decimal import Decimal
- root = Tk()
- width,height=400,400
- root.geometry("%dx%d+10+10"%(width,height))
- canvas = Canvas(root,width=width, height=height, background="grey" )
- canvas.grid()
- IMAGE_SIZE = 400
- PERLIN_RESOLUTION = 10
- GRADIENT = []
- for x in range(PERLIN_RESOLUTION + 1):
- GRADIENT.append([])
- for y in range(PERLIN_RESOLUTION + 1):
- angle = random.random() * 2 * math.pi
- vector = (
- Decimal(math.cos(angle)),
- Decimal(math.sin(angle))
- )
- GRADIENT[x].append(vector)
- def smoothstep(a0, a1, w):
- value = w*w*w*(w*(w*6 - 15) + 10)
- return a0 + value*(a1 - a0)
- def dotGridGradient(ix, iy, x, y):
- dx = x - Decimal(ix)
- dy = y - Decimal(iy)
- return (dx*GRADIENT[iy][ix][0] + dy*GRADIENT[iy][ix][1])
- def perlin(x, y):
- if x > 0.0:
- x0 = int(x)
- else:
- x0 = int(x) - 1
- x1 = x0 + 1
- if y > 0.0:
- y0 = int(y)
- else:
- y0 = int(y) - 1
- y1 = y0 + 1
- sx = x - Decimal(x0)
- sy = y - Decimal(y0)
- n0 = dotGridGradient(x0, y0, x, y)
- n1 = dotGridGradient(x1, y0, x, y)
- ix0 = smoothstep(n0, n1, sx)
- n0 = dotGridGradient(x0, y1, x, y)
- n1 = dotGridGradient(x1, y1, x, y)
- ix1 = smoothstep(n0, n1, sx)
- value = smoothstep(ix0, ix1, sy)
- return value
- image = Image.new('RGB', (IMAGE_SIZE, IMAGE_SIZE))
- pixels = image.load()
- for i in range(IMAGE_SIZE):
- x = Decimal(i) / IMAGE_SIZE
- for j in range(IMAGE_SIZE):
- y = Decimal(j) / IMAGE_SIZE
- value = perlin(x * 10, y * 10)
- greyscale = int((value + 1) * 255 / 2)
- pixels[i, j] = (greyscale, greyscale, greyscale)
- canvas.create_line((i, j, i+1, j+1), fill='#'+('%02x'%greyscale)*3)
- canvas.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement