Contents

首先 pip install Pillow 安装 Pillow 模块。(这名字挺萌的)

直接上代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# -*- coding: utf-8 -*-
import urllib
from io import BytesIO
from PIL import Image, ImageDraw, ImageFont
PIC_URL = "http://pic1.zhimg.com/3676e9bd6_l.jpg"
DRAW_WORD = u"①"
TRANSPARENT = (255, 255, 255, 0)
SOFT_RED = (243, 90, 74, 255)
rsp = urllib.urlopen(PIC_URL)
data = rsp.read()
fp = BytesIO()
fp.write(data)
fp.seek(0, 0)
base_image = Image.open(fp).convert('RGBA')
fnt = ImageFont.truetype('CALIBRI.TTF', base_image.size[0] / 4) # FreeTypeFont
fnt_size = fnt.getsize(DRAW_WORD)
txt_image = Image.new('RGBA', base_image.size, TRANSPARENT)
ImageDraw.Draw(txt_image).text((base_image.size[0] - fnt_size[0], 10), DRAW_WORD, fill=SOFT_RED, font=fnt)
out = Image.alpha_composite(base_image, txt_image)
out.show()

效果图:

参考资料:

  1. 图片来自 知乎
  2. RGBA color space
  3. Python 练习册,每天一个小程序

附上最近发现的一个好网站:Adobe Color CC

Contents