pyavatar
Create default user avatars from a given string
This package creates simple user avatars that can be used in web-applications.
Avatars are generated from the first letter of a given string input.
Installation
Pyavatar is on Pypi so all you need is:
pip install pyavatar
Usage
Generate an avatar
>>> from pyavatar import PyAvatar
>>>
>>> avatar = PyAvatar("smallwat3r", size=250)
>>> avatar = PyAvatar("smallwat3r", capitalize=False)
>>> avatar = PyAvatar("smallwat3r", color=(40, 176, 200))
>>> avatar = PyAvatar("smallwat3r", fontpath="/Users/me/fonts/myfont.ttf")
Change the avatar color
>>> avatar.color
(191, 91, 81)
>>> avatar.change_color()
>>> avatar.color
(203, 22, 126)
>>> avatar.change_color("#28b0c8")
>>> avatar.color
'#28b0c8'
Save the avatar as a base64 image
>>> image = avatar.base64_image("jpeg")
'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBg ...'
You can then render it in an html tag with Jinja or another template engine
<img src="{{ image }}" alt="My avatar" />
Or save it as a bytes array
>>> avatar.stream("png")
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\xfa\x00\x00 ...'
Or save it as a file locally
>>> import os
>>> avatar.save(f"{os.getcwd()}/me.png")
Development
Requirements
Make sure you're using a version of Python >= 3.10
- Create a virtual environment and install the dependencies
make venv deps
Sanity checks
-
Unit tests
make tests
-
Mypy
make mypy
-
Ruff
make ruff
-
Run all the above
make ci
Code formatting
We're using YAPF to format the code
make yapf
Release to Pypi
make release