In me my previous post, I wrote about creating Identicons using C# for .NET applications. In this short post, we'll create Identicons with Python.
Identicon is an image representation of values like a name, email, or a unique id. Popular websites including GitHub used identicon as the default user avatar.
Creating Identicons with Python is an easy task. In this example, I'll be using a small Python library called Pydenticon to make the task easy. This library can be used with frameworks with Django and Flask if you want to use Identicon as the default user avatar.
For more information on this library, please read the documentation here.
Install Pydenticon Library
The Pydenticon library can be installed using pip by running the following command.
pip install pydenticon
Before generating the image, the Identicon generator should be initialized with the following code.
import pydenticon # Instantiate a generator that will create 10x10 block identicons. generator = pydenticon.Generator(10, 10)
After initializing the generator, call generate method with the data to be converted as Identicon and the height and width of the resulting image in pixels.
# Generate a 240x240 PNG identicon. identicon = generator.generate("email@example.com", 240, 240)
The result can be obtained as an image by setting the output_format parameter to png.
identicon = generator.generate("firstname.lastname@example.org", 240, 240, output_format="png")
Then, write the file to disk.
f = open("identicon.png", "wb") f.write(identicon) f.close()