summaryrefslogtreecommitdiff
path: root/hemiptera_avatar_gen.py
diff options
context:
space:
mode:
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"
+
+