Python 练习册 => 0000 将 QQ 头像右上角加上红色的数字

练习心得

  • 使用PIL进行简单的图像处理
  • RGBA 不能转换为 JPEG

收藏一个字体下载站:http://www.touwenzi.com

源码如下:

https://github.com/plough/show-me-the-code/tree/master/0000

#!/usr/bin/env python
# encoding: utf-8

from PIL import Image, ImageDraw, ImageFont
# get an image
base = Image.open('image/headphoto.jpg').convert('RGBA')

# make a blank image for the text, initialized to transparent text color
txt = Image.new('RGBA', base.size, (255,255,255,0))

# get a font
fnt = ImageFont.truetype('经典综艺体简.ttf', 160)
# get a drawing context
d = ImageDraw.Draw(txt)

# draw text, full opacity
d.text((380,50), "4", font=fnt, fill=(255,8,28,255))

out = Image.alpha_composite(base, txt)

out.show()

# RGBA 不能转换为 JPEG
# out.save('result.jpg', 'JPEG')
out.save('result.png')