Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
# -*-encoding: utf-8 -*-
import argparse
import sys
from hashlib import md5
from PIL import Image, ImageDraw
def main():
# hash the input text until enough length we want
# only need half size of image, because it should be vertical symmetric
seed = ''
while len(seed) / 2 < (block_length[0] * block_length[1]):
seed += md5(input).hexdigest()
# prepare the color
seed_len = len(seed)
r = sum([ord(c) for c in seed[:seed_len/3]]) % 255
g = sum([ord(c) for c in seed[seed_len/3:2*seed_len/3]]) % 255
b = sum([ord(c) for c in seed[2*seed_len/3:]]) % 255
# iterate through each character in seed text
x = 0
y = 0
for c in seed:
# only paint half the image,
# other part should be vertical symmetry
if x < size / 2:
# if character is a digit then paint a color rectangle
# if not then paint a white rectangle
if c.isdigit():
((x, y), (x+block_size, y+block_size)),
(r, g, b))
x += block_size
x = 0
y += block_size
# get the image pixels
pixels = image.load()
# make the image vertical symmetry
for x in range(size/2):
for y in range(size):
pixels[size/2+x, y] = pixels[size/2-x, y]
# save the image as PNG file, 'png')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-o', '--output', help='output file name')
'-s', '--size', type=int, help='image size')
'-w', '--width', type=int, help='color rectangle width')
args = parser.parse_args()
# get input text
input = args.text
# get output file name if provided
if args.output:
output = args.output
output = '%s-avatar.png' % (input)
# get size and block width if provided
# size should be multiple of rectangle width
if args.size and args.width and args.size % args.width == 0:
size = args.size
block_size = size / args.width
elif any([args.size, args.width]):
size = 400
block_size = 40
block_length = (size/block_size, size/block_size)
# create a new Image instance
image ='RGB', (size, size), 'white')
draw = ImageDraw.Draw(image)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment