Skip to main content

ui-avatars

PyPI CI Coverage Downloads License

Deterministic avatar URLs for light and dark designs: Gravatar or Libravatar photos falling back to colored initials via ui-avatars.com. Same input, same URL, always.

Also available for PHP and JavaScript.

Install

pip install ui-avatars

Usage

from ui_avatars import avatar_url

avatar_url(name="Ada Lovelace", email="ada@example.com")
# Gravatar photo, falling back to generated initials

avatar_url(name="Ada Lovelace")
# straight ui-avatars.com URL, no Gravatar lookup

avatar_url(name="Ada Lovelace", email="ada@example.com", image=user.avatar)
# uploaded photo (e.g. Django FieldFile or URL), falling back to Gravatar or initials

avatar_url(email="ada@example.com")
# initials from the first two letters of the email

avatar_url(name="Ada Lovelace", alpha=0.75, size=256, rounded=True)
# pass any of the options as one-off overrides

avatar_url(name="Ada Lovelace", mask="hexagon", format="webp")
# crop the image to a hexagon and serve webp via wsrv.nl

avatar_url(name="Ada Lovelace", email="ada@example.com", source="libravatar")
# look up the photo on Libravatar instead of Gravatar

avatar_url(email="ada@example.com", host="example.com", source="secure.gravatar.com")
# override hostnames to use custom/self-hosted servers

Arguments

At least one of name or email is required.

Name Description
name Name used to generate initials
email Email address used to look up a photo via Gravatar or Libravatar
image Image URL or file object

Options

Options can be configured globally or overridden per call.

Name Default Description
alpha 0.2 Background opacity (0 to 1)
background Pin the background color
base Base URL for resolving relative image URLs
bold True Bold the initials
colors RAINBOW_500 List of hex values or (background, text) tuples
font_color Pin the text color
font_size 0.4 Size of the initials (0.1 to 1)
format Image format, e.g. png, svg, and other formats
hash sha256 Email hash for photo lookups, sha256 or md5
host ui-avatars.com User initials avatars host
length 2 Number of initials
mask Shape mask, e.g. hexagon, pentagon, square, and other masks
proxy wsrv.nl Proxy for masks, rounding, and extra formats
region Service region, e.g. eu or na (ignored if host is set)
rounded False Round the image (ignored if mask is set)
size 128 Image size in pixels
source gravatar Photo source, e.g. gravatar, libravatar, or any compatible host
uppercase True Uppercase the initials

Configuration

Configure the shared avatars instance that avatar_url() uses, or construct your own:

from ui_avatars import avatar_url, avatars, Avatars

avatars.configure(colors=["#1d4ed8", "#b91c1c"], size=256, rounded=True)
avatar_url(name="Ada Lovelace", email="ada@example.com")

my_avatars = Avatars(colors=["#1d4ed8", "#b91c1c"], size=256, rounded=True)
my_avatars.build(name="Ada Lovelace", email="ada@example.com")

Colors

The default color palette is tailwind_colors RAINBOW_500. Swap it for another scale:

from tailwind_colors import TCH

avatars.configure(colors=TCH.RAINBOW_300)
# or make up your own color palette
avatars.configure(colors=["#f00", "#0f0", "#00f"])

Pair colors manually with (background, text) tuples. Set alpha=1 for solid backgrounds:

avatars.configure(alpha=1, colors=[("#f00", "#fff"), ("#000", "#00f")])

To override the palette and force a specific color, pass the color argument:

avatar_url(name="Ada Lovelace", email="ada@example.com", color="#f00")

To pin the background color, text color, or both, pass background and font_color:

avatar_url(name="Ada Lovelace", alpha=1, background="#f00", font_color="#000")

Mixin

from ui_avatars import AvatarMixin, Avatars


class User(AvatarMixin, AbstractUser):
    pass


user.avatar_url

If you want to configure the instance, assign it to avatars on the model:

class User(AvatarMixin, AbstractUser):
    avatars = Avatars(size=64, source="libravatar")

Relative image URLs, such as Django's ImageField with the default storage, and images on local hosts, e.g. localhost, *.test or private IPs, can't be reached by the proxy, so they're served as they are, without cropping, masks, or a fallback. The base option makes relative URLs absolute:

class User(AvatarMixin, AbstractUser):
    profile_picture = models.ImageField(upload_to="profile_pictures", blank=True)

    avatars = Avatars(base="https://example.com")

By default, the mixin uses get_full_name(), full_name, name, first_name and last_name, or username for initials, and avatar, profile_picture, or image for images, in that order.

Override get_avatar_name(), get_avatar_email(), or get_avatar_image() if needed:

class User(AvatarMixin, AbstractUser):
    photo = models.ImageField(upload_to="photos", blank=True)

    def get_avatar_image(self):
        return self.photo

Development

poetry install
poetry run pytest
poetry run ruff check --fix

License

MIT

Metadata

Release files for ui-avatars 1.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ui-avatars 1.4.0
File Size Uploaded Provenance Actions
ui_avatars-1.4.0.tar.gz 11.8 kB With provenance Details for ui_avatars-1.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ui-avatars 1.4.0
File Interpreter ABI Platform Actions
ui_avatars-1.4.0-py3-none-any.whl Python 3 none any Details for ui_avatars-1.4.0-py3-none-any.whl

Total release size: 22.2 kB

Release files / ui_avatars-1.4.0.tar.gz

Download URL ui_avatars-1.4.0.tar.gz
Size 11.8 kB
Tags Source
SHA-256 checksum
How to use checksums
94fac338043c2ab92767bc61317dfbb8dc2db41a516a22400a59ccc891328231
BLAKE2b-256 checksum
How to use checksums
7b2ec0e1d2d5f3358a57e85e566c846ec1ac6e15768fca9f09bbbd7ff225dea7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 26, 2026.

Transparency log

Release files / ui_avatars-1.4.0-py3-none-any.whl

Download URL ui_avatars-1.4.0-py3-none-any.whl
Size 10.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3830477e9b8a85d5b749ab0b64ae9608167ebc8e9d94bed489593e4dc58d5499
BLAKE2b-256 checksum
How to use checksums
bbe7eefdcd39e0d36f1da28229beb2fc10b97c13b42fd5eb1dcce4780c28f6e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 26, 2026.

Transparency log

Security for ui-avatars 1.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for ui_avatars-1.4.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 26, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page