How To Create Your Own Avatar Using Python ?

- By Pratyush Mishra

·

3 min read

How To Create Your Own Avatar Using Python ?

Hi everyone👋 , 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.You can refer to my YouTube video to see a working tutorial for better understanding and a step-by-step guide of the same.

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: 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: github.com/tschoonj/GTK-for-Windows-Runtime.. 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 Python3
# importing the require package
from py_avataaars import PyAvataaar  

# assigning various parameters to our avatar
avatar = PyAvataaar()

# rendering the avatar in png format
avatar.render_png_file("AVATAR_1.png")

Output :

AVATAR1.png

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 by using PyAvataaar() method.

Syntax :

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)

Implementation :

# Python program to create custom avatars

# importing the require package
import py_avataaars as pa  

# assigning various parameters to our avatar
avatar = pa.PyAvataaar(style=pa.AvatarStyle.CIRCLE,
                       skin_color=pa.SkinColor.LIGHT,
                       hair_color=pa.HairColor.AUBURN,
                       facial_hair_type=pa.FacialHairType.MOUSTACHE_MAGNUM,
                       top_type=pa.TopType.SHORT_HAIR_SHAGGY_MULLET,
                       mouth_type=pa.MouthType.SCREAM_OPEN,
                       eye_type=pa.EyesType.SQUINT,
                       eyebrow_type=pa.EyebrowType.RAISED_EXCITED_NATURAL,
                       nose_type=pa.NoseType.DEFAULT,
                       accessories_type=pa.AccessoriesType.PRESCRIPTION_02,
                       clothe_type=pa.ClotheType.HOODIE,
                       clothe_graphic_type=pa.ClotheGraphicType.BAT,)

# rendering the avatar in png format
avatar.render_png_file("AVATAR_2.png")

Output :

AVATAR2.png

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.

Alright, guys! I hope this article was helpful for you if it is the leave your comments below. I will meet you in another article until then KEEP CODING🤗.

Â