Skip to content

Instantly share code, notes, and snippets.

@bryant
Created October 26, 2016 00:21
Show Gist options
  • Save bryant/ac78b67ee7d8a6b545d9128d12b898c7 to your computer and use it in GitHub Desktop.
Save bryant/ac78b67ee7d8a6b545d9128d12b898c7 to your computer and use it in GitHub Desktop.
generates a ppm of github identicon.
# what? i was bored.
class Identicon(object):
def __init__(self, bg, fg, border=35, block=71, count=5):
self.bg = bg
self.fg = fg
self.border = border
self.block = block
self.count = count
def gen(self, blockdat, size=None):
size = size or self.pixsize()
rv = [self.bg] * (size * size)
for y in xrange(0, size):
for x in xrange(0, size):
if self.border <= x < size - self.border and \
self.border <= y < size - self.border:
bx = (x - self.border) / self.block
by = (y - self.border) / self.block
if blockdat[by * self.count + bx]:
rv[y * size + x] = self.fg
return rv
def pixsize(self):
return self.border * 2 + self.block * self.count
def to_ppm(array, width, height):
fmt = """P3
%d %d
255
%s"""
pixdat = chunks(("%d %d %d" % a for a in array), width)
pixdat = "\n".join(" ".join(row) for row in pixdat)
return fmt % (width, height, pixdat)
def chunks(it, n):
from itertools import izip
i = iter(it)
its = [i] * n
return izip(*its)
def rgb(s):
s = int(s, 16)
return (s >> 16, (s >> 8) & 0xff, s & 0xff)
old = Identicon(rgb("f0f0f0"), rgb("d1d47c"))
new = Identicon(rgb("f0f0f0"), rgb("00356b"))
pat = [bool(int(c)) for c in "1010100000110110010010101"]
# "B"
block = [bool(int(c))
for c in "".join(('11110', '10001', '11110', '10001', '11110'))]
z = to_ppm(new.gen(pat), new.pixsize(), new.pixsize())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment