Skip to content

Instantly share code, notes, and snippets.

@cwillmor
Created June 13, 2018 05:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cwillmor/96ce85fdc13b0b4df4a1eb6c5f326b35 to your computer and use it in GitHub Desktop.
Save cwillmor/96ce85fdc13b0b4df4a1eb6c5f326b35 to your computer and use it in GitHub Desktop.
program for drawing @Cwillmore banner
# 05/02/18 04:46 PM
# program to draw twitter background
# might be a good foundation for beesandbombs-style animation?
from PIL import Image, ImageDraw
import os
import aggdraw
import random
import math
w = 1500
h = 500
def prob_density(x, y):
return math.exp(5 * x / w)
def sample():
x = random.uniform(0, w)
y = random.uniform(0, h)
score = prob_density(x, y)
for i in range(200):
xx = random.uniform(0, w)
yy = random.uniform(0, h)
new_score = prob_density(xx, yy)
should_step = new_score > score or random.uniform(0, 1) < new_score / score
if should_step:
x = xx
y = yy
score = new_score
return x, y
gray_bg = '#d5d5d7'
white_dot = '#fbfafb'
identity_transform = (1, 0, 0, 0, 1, 0)
transform_stack = [identity_transform]
def compose(t1, t2):
a1, b1, c1, d1, e1, f1 = t1
a2, b2, c2, d2, e2, f2 = t2
return (
a1 * a2 + b1 * d2,
a1 * b2 + b1 * e2,
a1 * c2 + b1 * f2 + c1,
d1 * a2 + e1 * d2,
d1 * b2 + e1 * e2,
d1 * c2 + e1 * f2 + f1
)
def append_transform(t):
transform_stack[-1] = compose(transform_stack[-1], t)
ctx.settransform(transform_stack[-1])
def translate(dx, dy):
append_transform((1, 0, dx, 0, 1, dy))
def rotate(theta):
s = math.sin(theta)
c = math.cos(theta)
append_transform((c, -s, 0, s, c, 0))
def scale(s):
append_transform((s, 0, 0, 0, s, 0))
# TODO: scale pen size too?
def pushstate():
transform_stack.append(transform_stack[-1])
def popstate():
transform_stack.pop()
ctx.settransform(transform_stack[-1])
img = Image.new("RGBA", (w, h))
ctx = aggdraw.Draw(img)
ctx.rectangle((0, 0, w, h), None, aggdraw.Brush(gray_bg))
num_points = 1000
for i in range(num_points):
x, y = sample()
theta = -math.pi/4 + random.normalvariate(0, math.pi/12)
#theta=math.pi/4
p = aggdraw.Pen(white_dot, 2)
pushstate()
translate(x, y)
rotate(theta)
scale(0.6)
ctx.line((0, 0, 8, 0), p)
if random.choice((True, False)):
ctx.line((-6, 10, 2, 10), p)
popstate()
ctx.flush()
img.save(os.path.expanduser("~/Desktop/out.png"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment