👨🏻🎓博主介绍:大家好,我是芝士味的椒盐,一名在校大学生,热爱分享知识,很高兴在这里认识大家🌟
🌈擅长领域:Java、大数据、运维、电子
🙏🏻如果本文章各位小伙伴们有帮助的话,🍭关注+👍🏻点赞+🗣评论+📦收藏,相应的有空了我也会回访,互助!!!
🤝另本人水平有限,旨在创作简单易懂的文章,在文章描述时如有错,恳请各位大佬指正,在此感谢!!!
@[TOC]
缘由
使用python的PIL.Image库生成含冬季元素的头像。
项目地址
gitee:https://gitee.com/mantianx/winter-olympics-avatar-generation
先贴一下头像生成的效果
代码展示
- 这里主要用到的库是PIL的image库极其的简单
WinterAvata.py
from PIL import Image class WinterAvata(Image.Image): # 自己的头像 def __init__(self, image_name): self.avatar_image = Image.open(image_name); self.avatar_image.convert('RGBA') self.avatar_image_width_rate = 0.08 # 模版层 def createHappyAvatar(self, default='beijing'): image = Image.open('avatar_template/winter_{}.png'.format(default)) image.convert('RGBA') image_width = int(image.size[0] * self.avatar_image_width_rate ) re_size_w, re_size_h = image.size[0] - image_width, image.size[1] - image_width avatar = self.avatar_image.resize((re_size_w, re_size_h)) temp_avatar = Image.new('RGBA', image.size) x1 = int(image_width / 2) y1 = int(image_width / 2) temp_avatar.paste(avatar, (x1, y1, x1 + re_size_w, y1 + re_size_h)) temp_avatar.paste(image, (0, 0, 2184, 2184), image) temp_avatar.save('result/resultAvatar.png')
main.py
import WinterAvata if __name__ == '__main__': winter_avatar = WinterAvata.WinterAvata('o1_ava.jpg') winter_avatar.createHappyAvatar('fireworks')