python_avatars
python_avatars copied to clipboard
Persistent avatar generation from input string
Is your feature request related to a problem? Please describe.
For example I want to add default avatar from username and it should be consistent when generated twice.
Describe the solution you'd like
username = "chiliec"
stableAvatar = pa.Avatar.random(from=username)
the functionality you're looking for can already be achieved using the current version of the package, you just need to write a couple of functions:
import python_avatars as pa
def get_nth_enum_item(n, e):
items = e.get_all()
return items[n % len(items)]
def avatar_from_username(username):
username_hash = hash(username)
return pa.Avatar(
style=get_nth_enum_item(username_hash, pa.AvatarStyle),
background_color=get_nth_enum_item(username_hash, pa.BackgroundColor),
top=get_nth_enum_item(username_hash, pa.TopType),
hat_color=get_nth_enum_item(username_hash, pa.ClothingColor),
eyebrows=get_nth_enum_item(username_hash, pa.EyebrowType),
eyes=get_nth_enum_item(username_hash, pa.EyeType),
nose=get_nth_enum_item(username_hash, pa.NoseType),
mouth=get_nth_enum_item(username_hash, pa.MouthType),
facial_hair=get_nth_enum_item(username_hash, pa.FacialHairType),
skin_color=get_nth_enum_item(username_hash, pa.SkinColor),
hair_color=get_nth_enum_item(username_hash, pa.HairColor),
facial_hair_color=get_nth_enum_item(username_hash, pa.HairColor),
accessory=get_nth_enum_item(username_hash, pa.AccessoryType),
clothing=get_nth_enum_item(username_hash, pa.ClothingType),
clothing_color=get_nth_enum_item(username_hash, pa.ClothingColor),
shirt_graphic=get_nth_enum_item(username_hash, pa.ClothingGraphic),
shirt_text=username,
title=f"Avatar for {username}",
)
And avatar_from_username('chiliec') should always generate something like this:
@ibonn thanks a lot! What do you think about include this feature inside package?
Hmmm... probably not. I may create a separate branch and keep this solution there just in case (I might change my mind in the future)