Skip to content
Related Articles

Related Articles

How to create your own Avatar using Python ?
  • Last Updated : 24 Feb, 2021

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. 



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:

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.

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:



Python3




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

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.

Demonstration: 


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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :