From c35389d77608fa78affe4eee331970897707bc97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20P=C3=A9rez-Cerezo?= Date: Fri, 6 Jul 2018 17:34:38 +0200 Subject: first commit --- hemiptera_avatar_gen.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 hemiptera_avatar_gen.py (limited to 'hemiptera_avatar_gen.py') diff --git a/hemiptera_avatar_gen.py b/hemiptera_avatar_gen.py new file mode 100644 index 0000000..db380a9 --- /dev/null +++ b/hemiptera_avatar_gen.py @@ -0,0 +1,52 @@ +import hashlib +import random +from PIL import Image, ImageDraw +from hemiptera_common import * +import os + +SERVER_SALT="foobar" + +D = 7 +pixel_width=1 +def hash_generator(h) : + index = 0 + while True : + yield int(h[index],16) + index +=1 + if index == len(h) : ## Keep images interesting if size is significantly larger than hash input + index = 0 + h = hashlib.sha1(h.encode()).hexdigest() + + +def gen_color(h) : + return tuple(tuple(next(h)*16 for i in range(3)) for i in range(2)) + +def gen_image(h) : + h = hash_generator(h) + c1, c2 = gen_color(h) + im = Image.new("RGBA", (D,D)) + draw = ImageDraw.Draw(im) + index = 0 + for i in range(D//2+1) : + for j in range(D) : + k = next(h) + if k % 2 or k//2 % 2 : + if k % 2 : + color = c1 + else : + color = c2 + draw.point([(i,j), (D-1-i,j)], fill=color) + return im + + +def generate_avatar(email) : + if email is None : + email = "" + h = hashlib.sha1(str(email+SERVER_SALT).encode()).hexdigest() + imgpath = opj(basedir, "export", "static") + img = gen_image(h) + os.makedirs(imgpath, exist_ok=True) + img.save(opj(imgpath, h+".png")) + return "/static/" + h+".png" + + -- cgit v1.2.3