Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persistent avatar generation from input string #5

Open
chiliec opened this issue Jan 31, 2024 · 3 comments
Open

Persistent avatar generation from input string #5

chiliec opened this issue Jan 31, 2024 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@chiliec
Copy link

chiliec commented Jan 31, 2024

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)
@ibonn ibonn added the enhancement New feature or request label Feb 2, 2024
@ibonn ibonn self-assigned this Feb 2, 2024
@ibonn
Copy link
Owner

ibonn commented Feb 3, 2024

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

@chiliec
Copy link
Author

chiliec commented Feb 5, 2024

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

@ibonn
Copy link
Owner

ibonn commented Feb 28, 2024

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants