Skip to content

Commit 3e0eb4a

Browse files
author
Will
authored
Create add_num.py
finish no.0000
1 parent 624a843 commit 3e0eb4a

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

will/0000/add_num.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# 第 0000 题:将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果
2+
'''
3+
涉及模块/库:
4+
1.Pillow :http://pillow-cn.readthedocs.io/zh_CN/latest/
5+
2.random
6+
核心方法
7+
1.ImageFont Module:
8+
PIL.ImageFont.truetype(font=None, size=10, index=0, encoding='', filename=None)
9+
2.ImageDraw Module:
10+
PIL.ImageDraw.Draw.text(xy, text, fill=None, font=None, anchor=None)
11+
想法思路:
12+
1.Pillow库所读的图片以左上角为原点,类似于第四象限
13+
2.数字添加在图片的右上角,注意控制图片的位置,及xy参数
14+
'''
15+
16+
17+
import random
18+
from PIL import Image, ImageDraw, ImageFont
19+
new = 'new.jpg'
20+
21+
def add_num(pic, text):
22+
'add a message number on pics'
23+
try:
24+
im = Image.open(pic)
25+
except:
26+
print('failed')
27+
width, height = im.size
28+
fontsize = height / 4
29+
draw = ImageDraw.Draw(im)
30+
font = ImageFont.truetype('arial.ttf', int(fontsize))
31+
draw.text((0.7*width, 0.02*height), text, font=font, fill='red')
32+
im.save(new)
33+
34+
if __name__ == "__main__":
35+
number = str(random.randint(1, 99))
36+
pic = './old.jpg'
37+
add_num(pic, number)

0 commit comments

Comments
 (0)