python_avatars icon indicating copy to clipboard operation
python_avatars copied to clipboard

Persistent avatar generation from input string

Open chiliec opened this issue 1 year ago • 3 comments

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)

chiliec avatar Jan 31 '24 11:01 chiliec

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: avatar

ibonn avatar Feb 03 '24 13:02 ibonn

@ibonn thanks a lot! What do you think about include this feature inside package?

chiliec avatar Feb 05 '24 18:02 chiliec

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)

ibonn avatar Feb 28 '24 18:02 ibonn