Identicons are now generated by ivatar and switch retro to a more modern version - but only does 5x5 (like GitHub)

This commit is contained in:
Oliver Falk
2019-02-28 12:21:02 +01:00
parent af9f3f9a1a
commit f7f573e99d
2 changed files with 26 additions and 30 deletions

View File

@@ -16,7 +16,9 @@ from django.urls import reverse_lazy
from PIL import Image from PIL import Image
from monsterid.id import build_monster as BuildMonster from monsterid.id import build_monster as BuildMonster
from pydenticon import Generator as IdenticonGenerator import Identicon
from pydenticon5 import Pydenticon5
from robohash import Robohash from robohash import Robohash
from ivatar.settings import AVATAR_MAX_SIZE, JPEG_QUALITY, DEFAULT_AVATAR_SIZE from ivatar.settings import AVATAR_MAX_SIZE, JPEG_QUALITY, DEFAULT_AVATAR_SIZE
@@ -120,8 +122,8 @@ class AvatarImageView(TemplateView):
# Return the default URL, as specified, or 404 Not Found, if default=404 # Return the default URL, as specified, or 404 Not Found, if default=404
if default: if default:
# Proxy to gravatar to generate wavatar/identicon- lazy me # Proxy to gravatar to generate wavatar - lazy me
if str(default) == 'wavatar' or str(default) == 'identicon': if str(default) == 'wavatar':
url = reverse_lazy('gravatarproxy', args=[kwargs['digest']]) \ url = reverse_lazy('gravatarproxy', args=[kwargs['digest']]) \
+ '?s=%i' % size + '&default=%s&f=y' % default + '?s=%i' % size + '&default=%s&f=y' % default
return HttpResponseRedirect(url) return HttpResponseRedirect(url)
@@ -154,31 +156,24 @@ class AvatarImageView(TemplateView):
content_type='image/png') content_type='image/png')
if str(default) == 'retro': if str(default) == 'retro':
# Taken from example code identicon = Identicon.render(kwargs['digest'])
foreground = [ data = BytesIO()
'rgb(45,79,255)', img = Image.open(BytesIO(identicon))
'rgb(254,180,44)', img = img.resize((size, size), Image.ANTIALIAS)
'rgb(226,121,234)', img.save(data, 'PNG', quality=JPEG_QUALITY)
'rgb(30,179,253)', data.seek(0)
'rgb(232,77,65)', return HttpResponse(
'rgb(49,203,115)', data,
'rgb(141,69,170)'] content_type='image/png')
background = 'rgb(224,224,224)'
padwidth = int(size/10) if str(default) == 'identicon':
if padwidth < 10: p = Pydenticon5()
padwidth = 10 # In order to make use of the whole 32 bytes digest, we need to redigest them.
if size < 60: newdigest = hashlib.md5(bytes(kwargs['digest'], 'utf-8')).hexdigest()
padwidth = 0 img = p.draw(newdigest, size, 0)
padding = (padwidth, padwidth, padwidth, padwidth) data = BytesIO()
# Since padding is _added_ around the generated image, we img.save(data, 'PNG', quality=JPEG_QUALITY)
# need to reduce the image size by padding*2 (left/right, top/bottom) data.seek(0)
size = size - 2*padwidth
generator = IdenticonGenerator(
10, 10, digest=hashlib.sha1,
foreground=foreground, background=background)
data = generator.generate(
kwargs['digest'], size, size,
output_format='png', padding=padding, inverted=False)
return HttpResponse( return HttpResponse(
data, data,
content_type='image/png') content_type='image/png')
@@ -247,7 +242,7 @@ class GravatarProxyView(View):
except: except:
pass pass
if str(default) != 'wavatar' and str(default) != 'identicon': if str(default) != 'wavatar':
# This part is special/hackish # This part is special/hackish
# Check if the image returned by Gravatar is their default image, if so, # Check if the image returned by Gravatar is their default image, if so,
# redirect to our default instead. # redirect to our default instead.

View File

@@ -33,6 +33,7 @@ mysqlclient
psycopg2 psycopg2
notsetuptools notsetuptools
git+https://github.com/ofalk/monsterid.git git+https://github.com/ofalk/monsterid.git
git+https://github.com/azaghal/pydenticon.git
git+https://github.com/ofalk/Robohash.git@devel git+https://github.com/ofalk/Robohash.git@devel
python-memcached python-memcached
git+https://github.com/ercpe/pydenticon5.git
git+https://github.com/flavono123/identicon.git