In this article, we will discuss how to create a custom avatar using Python. In order to perform this task. You don’t have to create it yourself on software or download it from some website, but a few lines of code will help you to generate an avatar of your choice.
We will be using the py-avataaars package from which we will be creating those beautiful avatars. The basic syntax of this module is:
from py_avataaars import PyAvataaar avatar = PyAvataaar() avatar.render_png_file('<output_file.png>')
Here the PyAvataaar Class is in the __init__.py which is responsible for a number of functionalities, out of which the major one is setting the parameters of the avatar for eg: change in skin color, outfit, eyes, hairs, hair colors, moods etc.
So, let’s begin…
Step 1: Installation of some packages.
- First type pip install py-avataaars in your terminal to install the avatars package (Check: https://github.com/kebu/py-avataaars for more information)
- Then download and install this gtk file from here gtk3-runtime-3.24.24-2021-01-30-ts-win64.exe. Your antivirus will try to block it, but you should allow it to download for the code to run properly, trust me guys it’s a non-harmful file (Check: https://github.com/tschoonj/GTK-for-Windows-Runtime-Environment-Installer for more information)
Step 2: Writing the python program.
- First, we will try to generate the default avatar by writing this code and see if this works properly or not
The above program will generate the AVATAR_1.png file in the folder where you’ve kept this python program. Once the above program is running properly, then we will generate the avatars according to our needs with this by using PyAvataaar() method.
PyAvataaar(style, skin_color, hair_color, facial_hair_type, top_type, mouth_type, eye_type, eyebrow_type, nose_type, accessories_type, clothe_type, clothe_graphic_type)
You can always change the parameters of the avatar accordingly by pressing the Ctrl button and hovering over the py_avataaars line, which will turn it blue and then you can click on it to see __init__.py file, where you can find all the parameters, each written in a different class.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.