User:Thierry Dugnolle/Python/Cantor Set Expansion

print("Cantor Set Expansion")

from PIL import Image, ImageDraw

import math

imWidth = 1800
expand = 200
spWidth = imWidth*expand
imHeight = 180
imNumber = 300
depth = 12
width = 1.0
start = 0.0

im = Image.new("L", (imWidth, imHeight), 0)
px = im.load()
cantor = []
cantor = [0 for i in range(spWidth+1)]
Scantor = []
Scantor = [0.0 for i in range(imWidth)]
Scantor2 = []
Scantor2 = [0.0 for i in range(imWidth)]

def draw(depth, width, start):
    x1 = math.floor(start * spWidth)
    x2 = math.floor((start + width) * spWidth)
    cantor[x1] = 1
    cantor[x2] = 1
    width = width / 3.0
    depth = depth - 1
    if depth > 0:
        draw(depth, width, start)
        draw(depth, width, start + 2.0 * width)

draw(depth, width, start)

for imnum in range(imNumber):
    zoom=pow(3.0,imnum/imNumber)
    # reduction to the size of the image
    for i in range(imWidth):
        Scantor[i]=0.0
        for k in range(expand):
            Scantor[i]+= cantor[math.floor((i*expand+k)/zoom)]

    # smoothing
    Scantor2[0]=(Scantor[0] + 0.5 * Scantor[1])/2.0
    Scantor2[imWidth-1] = (Scantor[imWidth-1] + 0.5 * Scantor[imWidth-2]) / 2.0
    brightMax = max(Scantor2[0],Scantor2[imWidth-1])
    for i in range(1,imWidth-1):
        Scantor2[i]= (0.5 * Scantor[i-1] + Scantor[i] + 0.5 * Scantor[i+1])/2.0
        if Scantor2[i] > brightMax:
            brightMax = Scantor2[i]
    # drawing
    for i in range(imWidth):
        for j in range(imHeight):
            px[i, j] = math.floor(255 * Scantor2[i]/brightMax)
    im.save("Cantor" + str(1000 + imnum) + ".bmp")

print("Good bye")