draw('.*1.*')
Produces:
import re, itertools | |
from PIL import Image | |
def str2pt(s): | |
L = len(s) - 1 | |
x, y = 1, 1 | |
for n in s: | |
y += 2**L if n in '34' else 0 | |
x += 2**L if n in '14' else 0 | |
L -= 1 | |
return x-1, y-1 | |
def generate(n): | |
return map(lambda x: ''.join(x), itertools.product('1234', repeat=n)) | |
def fractal(r, n): | |
return map(str2pt, (c for c in generate(n) if re.match(r, c))) | |
def draw(r, n=10, filename='/tmp/fractal.png'): | |
im = Image.new('RGB', (2**n, 2**n), "black") | |
for x, y in fractal(r, n): | |
im.putpixel((x, y), (255, 255, 255,)) | |
im.save(filename, 'PNG') |