if w < 1: return for x in range(3): for y in range(3): if x == 1 and y == 1: c = max(0, 0.5 - 0.5*w/W) context.fill((random(), c, c)) #print(x, y, w, 0.5*w/W) context.rect(px+w, py+w, w, w) elif px <= maxW and py <= maxW: drawSierpinskiSquare(px+x*w, py+y*w, w/3.0, maxW) context.newDocument(w=W, h=H) context.newPage(W, H) context.fill((1, 1, 1)) context.rect(0, 0, W, H) drawSierpinskiSquare(0, 0, W, W) # TODO: Check on sensitivity of Flat for color types in relation to document types # Gray scale does not work for .svg # Opaque does not work for .pdf # Context should hide that problem. for extension in ('pdf', 'jpg'): if not os.path.exists('_export'): os.mkdir('_export') exportPath = "_export/SierpinskiSquare." + extension context.saveDocument(exportPath) os.system(u'open "%s"' % exportPath) print(('Done'))
W = H = 500 PAGES = 3 RECTS = 150 R = 20 # Diameter of circle or square M = 20 # Page margin context.newDocument(w=W, h=H, title=TITLE, pageCount=3) for p in range(PAGES): context.newPage(W, H) for n in range(RECTS): # Compatible to DrawBot: color values between (0, 1) red, green, blue = random(), random(), random() #a = 0.5 + random()*0.5 #context.fill((red, green, blue, a)) context.fill((red, green, blue)) ch = random() x = M + random() * (W - 2 * M - R) # Only in available space y = M + random() * (H - 2 * M - R) if ch < 0.2: context.oval(x, y, R, R) elif ch < 0.4: context.rect(x, y, R, R) else: # Make formatted Flat flavor BabelString instance. bs = context.newString('Hello world on %d,%d' % (x, y), style=dict(font=font, fontSize=10)) context.text(bs, (x, y)) #context.saveImage('_export/HelloCircleSquare.gif') # PDF does not support alpha colors.
# P A G E B O T # # Licensed under MIT conditions # # Supporting usage of DrawBot, www.drawbot.com # Supporting usage of Flat, https://github.com/xxyxyz/flat # ----------------------------------------------------------------------------- # # AnimatedGif.py # # Run as cmd-line: # --> python AnimatedGif.py from random import random from pagebot.contexts.flatcontext import FlatContext context = FlatContext() print('Context class is %s' % type(context).__name__) W = 400 H = 400 context.newDocument(w=W, h=H) for n in range(1, 50): context.newPage(W, H) context.fill(random(), random(), random()) context.rect(random() * W, random() * H, 20, 20) context.saveImage('test.gif')