summaryrefslogtreecommitdiff
path: root/hemiptera_avatar_gen.py
diff options
context:
space:
mode:
authorGabriel Pérez-Cerezo <gabriel@gpcf.eu>2018-07-06 17:34:38 +0200
committerGabriel Pérez-Cerezo <gabriel@gpcf.eu>2018-07-06 17:34:38 +0200
commitc35389d77608fa78affe4eee331970897707bc97 (patch)
treec5edaedc030c5b5025e78aafd1ab22d0bba8b04a /hemiptera_avatar_gen.py
downloadhemiptera-c35389d77608fa78affe4eee331970897707bc97.zip
hemiptera-c35389d77608fa78affe4eee331970897707bc97.tar.gz
hemiptera-c35389d77608fa78affe4eee331970897707bc97.tar.bz2
first commit
Diffstat (limited to 'hemiptera_avatar_gen.py')
-rw-r--r--hemiptera_avatar_gen.py52
1 files changed, 52 insertions, 0 deletions
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"
+
+