Welcome, guest | Sign In | My Account | Store | Cart

Creates a surface from a grid of random numbers using Bilinear Interpolation.

Python, 29 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Random Surface Using Bilinear Interpolation
# http://en.wikipedia.org/wiki/Bilinear_interpolation
# FB36 - 20130222
import random
from PIL import Image, ImageDraw
imgx = 800; imgy = 600 # image size
image = Image.new("RGB", (imgx, imgy))
draw = ImageDraw.Draw(image)
pixels = image.load()
n = random.randint(2, 10); m = random.randint(2, 10) # grid size
ar = [[random.random() for i in range(n)] for j in range(m)] # random grid
nx = imgx / (n - 1.0)
ny = imgy / (m - 1.0)
for ky in range(imgy):
    for kx in range(imgx):
        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            
        c = int(z * 255)
        pixels[kx, ky] = (0, 0, c)

label = "N = " + str(n) + " M = " + str(m)
draw.text((0, 0), label, (0, 255, 0)) # write to top-left using green color
image.save("RandomSurface.png", "PNG")

1 comment

Martin Miller 11 years ago  # | flag

A more interesting (or a least more colorful) image can be created by using the colorsys module to compute the color of each pixel based on its z value:

pixels[kx, ky] = tuple(int(v*256) for v in colorsys.hsv_to_rgb(z, 1, 1))