本文包含的练习题主要是PIL依赖库,即pillow相关的应用。
练习一:使用python给图片增加数字
实现思路:
- 使用PIL的Image.open导入图片。
- 获取图片的大小。
- 调用ImageDraw,在图片的指定位置写上数字。
#coding=utf-8
#Auther by Alice
#在图片的右上角增加一个数字
from PIL import Image,ImageFont,ImageDraw
image = Image.open('/Users/alice/Documents/Photo/IMG_8379.JPG')
#打开原图
wight, hight = image.size
text = "015"
color = (255,255,0)
fontsize = wight//10
font = ImageFont.truetype('Apple Symbols',fontsize)
#设定增加的数字的参数,数字内容、数字颜色和数字字号
draw = ImageDraw.Draw(image)
draw.text((fontsize*6,0), text, color, font)
image.save('/Users/alice/Documents/Photo/IMG_7997.JPG', 'jpeg')
#保存添加了数