第 0000 题: 将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果。 类似于图中效果
Python练习册,每天一个小程序:https://github.com/Yixiaohan/show-me-the-code
前提:
PIL:Python Imaging Library
由于PIL仅支持到Python 2.7,加上年久失修,于是一群志愿者在PIL的基础上创建了兼容的版本,名字叫Pillow,支持最新Python 3.x
声明:
代码copy自汉森x, 自己只是加了大量注释. 侵删.
from PIL import Image, ImageFont, ImageDraw
def white_to_transparent(img):
"""抠图"""
#convert()函数,用于不同模式图像之间的转换。
#PIL中有九种不同模式,分别为1,L,P,RGB,RGBA,CMYK,YCbCr,I,F。
#模式“1”为二值图像,非黑即白。模式“L”为灰色图像.模式“P”为8位彩色图像.模式“RGBA”为32位彩色图像.
#模式“I”为32位整型灰色图像.模式“F”为32位浮点灰色图像.
img = img.convert('RGBA') #返回一个转换后的图像副本
datas = img.getdata() #像素数据队列
newData = []
for item in datas:
if item[0] == 255 and item[1] == 255:
newData.append((255,255,255,0)) #把白色变为透明色
else:
newData.append(item)
img.putdata(newData) #赋值给图片新的像素数据
img.save("change.png","PNG")
return img
if __name__ == "__main__":
p1_name = "github_logo" #the path of image.
p2_name = "red_reminder"
p1_image = Image.open(p1_name) #make a Image object
p2_image = Image.open(p2_name)
p2_transparent=white_to_transparent(p2_image)
p1_image.paste(p2_transparent,(0,0),p2_transparent) #paste方法?
#usr_font=ImageFont.truetype("usr/share/fonts/truetype\
# /deepin/DeepinOpenSymbol.ttf",32)
usr_font = ImageFont.truetype("./FZZCHJW.TTF",32)
#If"can't open resource" it means, python can't find your font.
#Use absolute path, rather than relative path.
draw=ImageDraw.Draw(p1_image) #在p1_image上绘制文字,图像
draw.text((152,8),u'10',font=usr_font)
p1_image.save("final.png","PNG")
final.png
red_reminder
github_logo
以及一个字体文件, 可以选用系统自带的字体.