{"id":18026424,"url":"https://github.com/aymanemx/default-avatars","last_synced_at":"2025-03-27T01:31:11.922Z","repository":{"id":111422195,"uuid":"293620501","full_name":"aymaneMx/default-avatars","owner":"aymaneMx","description":"This a demo of how to generate cool avatars from a given string (email, username...) or from your logo using python.","archived":false,"fork":false,"pushed_at":"2020-09-07T19:59:15.000Z","size":6,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-22T21:05:26.762Z","etag":null,"topics":["avatars","initials","initials-avatar","python"],"latest_commit_sha":null,"homepage":"https://aymanemx.com/posts/fancy-avatars-in-python","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aymaneMx.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-09-07T19:58:53.000Z","updated_at":"2023-01-03T18:43:30.000Z","dependencies_parsed_at":"2023-03-27T22:17:54.675Z","dependency_job_id":null,"html_url":"https://github.com/aymaneMx/default-avatars","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aymaneMx%2Fdefault-avatars","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aymaneMx%2Fdefault-avatars/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aymaneMx%2Fdefault-avatars/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aymaneMx%2Fdefault-avatars/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aymaneMx","download_url":"https://codeload.github.com/aymaneMx/default-avatars/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245764648,"owners_count":20668455,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["avatars","initials","initials-avatar","python"],"created_at":"2024-10-30T08:06:41.555Z","updated_at":"2025-03-27T01:31:11.913Z","avatar_url":"https://github.com/aymaneMx.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# The Easiest Way To Generate Fancy Avatars In Python\n\nThis a demo of my [article](https://aymanemx.com/posts/fancy-avatars-in-python) on how to generate cool avatars from a given string (email, username...) or from your logo using python.\n\n## Run it locally \n\n```bash\npipenv shell \n\npython local_test.py \n```\n\n## Django integration \n\nCreate a file `utils/avatars.py` in your django project, paste this in it :point_down:\n\n```python\n# BUILT-IN\nimport random\nimport re\nfrom io import BytesIO\n\n# PIP\nfrom cairosvg import svg2png\nfrom xml.sax.saxutils import escape as xml_escape\nfrom django.core.files.images import ImageFile\n\n\nCOLORS = [\n    ['#DF7FD7', '#DF7FD7', '#591854'],\n    ['#E3CAC8', '#DF8A82', '#5E3A37'],\n    ['#E6845E', '#E05118', '#61230B'],\n    ['#E0B050', '#E6CB97', '#614C23'],\n    ['#9878AD', '#492661', '#C59BE0'],\n    ['#787BAD', '#141961', '#9B9FE0'],\n    ['#78A2AD', '#104F61', '#9BD1E0'],\n    ['#78AD8A', '#0A6129', '#9BE0B3'],\n]\n\nINITIALS_SVG_TEMPLATE = \"\"\"\n\u003csvg xmlns=\"http://www.w3.org/2000/svg\" pointer-events=\"none\" width=\"200\" height=\"200\"\u003e\n  \u003cdefs\u003e\n    \u003clinearGradient id=\"grad\"\u003e\n    \u003cstop offset=\"0%\" stop-color=\"{color1}\" /\u003e\n    \u003cstop offset=\"100%\" stop-color=\"{color2}\" /\u003e\n    \u003c/linearGradient\u003e\n  \u003c/defs\u003e\n  \u003crect width=\"200\" height=\"200\" rx=\"0\" ry=\"0\" fill=\"url(#grad)\"\u003e\u003c/rect\u003e\n  \u003ctext text-anchor=\"middle\" y=\"50%\" x=\"50%\" dy=\"0.35em\"\n        pointer-events=\"auto\" fill=\"{text_color}\" font-family=\"sans-serif\"\n        style=\"font-weight: 400; font-size: 80px\"\u003e{text}\u003c/text\u003e\n\u003c/svg\u003e\n\"\"\".strip()\nINITIALS_SVG_TEMPLATE = re.sub('(\\s+|\\n)', ' ', INITIALS_SVG_TEMPLATE)\n\n\ndef generate_avatar(text):\n    \"\"\"Generates a django ImageFile object based on an svg code with random\n    color chosen from a list of defined colors\"\"\"\n\n    initials = ':)'\n\n    text = text.strip()\n    if text:\n        split_text = text.split(' ')\n        if len(split_text) \u003e 1:\n            initials = split_text[0][0] + split_text[-1][0]\n        else:\n            initials = split_text[0][0]\n\n    random_color = random.choice(COLORS)\n    svg_avatar = INITIALS_SVG_TEMPLATE.format(**{\n        'color1': random_color[0],\n        'color2': random_color[1],\n        'text_color': random_color[2],\n        'text': xml_escape(initials.upper()),\n    }).replace('\\n', '')\n\n    stream = BytesIO()\n    svg2png(svg_avatar, write_to=stream)\n\n    return ImageFile(stream, name=text + '.png')\n```\nNow, the function `generate_avatar` is ready to be used in your project.\n\nEnjoy!","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faymanemx%2Fdefault-avatars","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faymanemx%2Fdefault-avatars","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faymanemx%2Fdefault-avatars/lists"}