GithubHelp home page GithubHelp logo

lsdlab / avatar-gen Goto Github PK

View Code? Open in Web Editor NEW
34.0 4.0 5.0 12.8 MB

pillow 生成头像,中英文首字母或者随机像素化。Using pillow for generate avatars, first letter of string in Chinese and English or random pixel like avatars.

License: MIT License

Python 98.13% Dockerfile 1.87%
python3 pillow avatar avatar-generator avatar-gen flask flask-docker flask-application flask-api microservice

avatar-gen's Introduction

avatar-gen

Build Status Build Status

pillow 头像生成,中文或英文首字母或者随机像素化。

Using pillow for generate avatars, first letter of string in Chinese and English or random pixel like avatars.

参考了 https://github.com/maethor/avatar-generatorhttps://github.com/richardasaurus/randomavatar

第一个库用的字体不能生成中文,换成了思源黑体,然后位置也调整了一下才能把字放在中间。

Flask API

参考 app.py,用了 celery,再 Docker 化做成微服务。

python app.py    # python 单进程
gunicorn app:app -c gunicorn.conf    # four worker and gevent
celery -A app.celery worker --loglevel=info --autoscale=4,2     # celery job queue

Docker 运行

用 Docker 运行请注意 app.py 里面的 app.config.update() Redis 作为 Celery 的 Broker, redis host 用 docker-compose.yml 的 container_name avatar-gen-redis

docker-compose up

两个 API,都是 GET 方法,filetype 参数可以不传:

curl -X GET 'http://localhost:5000/api/v1/letter_avatar?size=128&[email protected]&filetype=PNG'
curl -X GET 'http://localhost:5000/api/v1/pixel_avatar?size=128&[email protected]&filetype=PNG'

Python 项目包安装方法 Python project package Installation:

pip install git+https://github.com/lsdlab/avatar-gen.git

使用方法 Usage:

生成出来的是 image byte array,自己用 PIL 保存。

generate result is image byte array, you need to save to file using PIL.

import io
from PIL import Image
from avatar_gen.letter_avatar import LetterAvatar
from avatar_gen.pixel_avatar import PixelAvatar

# generate letter avatar in Chinese or Enginlish character
image_byte_array = LetterAvatar.generate(size=128, string="[email protected]", filetype="PNG")
# image_byte_array = LetterAvatar.generate(size=128, string="[email protected]", filetype="PNG")
file_path = "/Users/Chen/PythonProjects/avatar-gen/letter_avatar.png"
image = Image.open(io.BytesIO(image_byte_array))
image.save(file_path)

# generate pixel avatar
pixel_avatar = PixelAvatar(rows=10, columns=10)
image_byte_array = pixel_avatar.get_image(size=128, string="[email protected]", filetype="PNG")
file_path = "/Users/Chen/PythonProjects/avatar-gen/pixel_avatar.png"
image = Image.open(io.BytesIO(image_byte_array))
image.save(file_path)
# or you can save image file using instance function
# return pixel_avatar.save(
    # image_byte_array=image_byte_array, save_location='pixel_avatar.png')

生成示例 examples:

Test

pytest

output:

Test session starts (platform: darwin, Python 3.6.3, pytest 3.5.0, pytest-sugar 0.9.1)
rootdir: /Users/Chen/PythonProjects/avatar-gen, inifile:
plugins: sugar-0.9.1, flask-0.10.0, django-3.1.2

 test_avatar_gen.py ✓✓                                                     100% ██████████

Results (0.16s):
       2 passed

avatar-gen's People

Contributors

lsdlab avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

avatar-gen's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.