Python 练习册 0-头像右上角添加数字
2016-04-14 17:36
573 查看
Python 练习册,每天一个小程序,原题来自Yixiaohan/show-me-the-code
我的代码仓库在Github
类似于图中效果:
在命令行中通过pip安装
如果想要详细了解Pillow的使用,请参考Pillow 官方文档
我的代码仓库在Github
目标
将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果。类似于图中效果:
安装PIL
PIL:Python Imaging Library,Python平台上的图像处理库。PIL功能强大,而且API简单易用。但是PIL仅支持到Python 2.7,有人在PIL的基础上创建了兼容的版本,名字叫Pillow,支持最新Python3.x,又加入了许多新特性,因此,我们可以直接安装使用Pillow。
在命令行中通过pip安装
$ pip install pillow
解决方案
该题目的代码实现如下:# 引入Pillow from PIL import Image, ImageDraw, ImageFont, ImageColor def add_num(img): # 创建一个Draw对象 draw = ImageDraw.Draw(img) # 创建一个 Font myfont = ImageFont.truetype('C:/windows/fonts/Arial.ttf', size=40) fillcolor = ImageColor.colormap.get('red') width, height = img.size draw.text((width-30, 0), '1', font=myfont, fill=fillcolor) img.save('result.jpg', 'jpeg') return 0 if __name__ == '__main__': image = Image.open('test.jpg') add_num(image)
如果想要详细了解Pillow的使用,请参考Pillow 官方文档
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法