#功能:输入图片完整地址,再输入新生成图片名字可以产生新的添加了数字的图片,其中字体文件我放置在代码同一文件夹下
# -*- coding: utf-8 -*-
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
#输入图片的完整路径,打开该图片
location = raw_input("Please input the location of the image:")
target = raw_input("Please input the new name of target, you can enter to get the default name:")
image = Image.open(location)
#获得当前图片的大小,根据此参数设置字体大小
xSize, ySize = image.size
font = ImageFont.truetype("arial.ttf",int(ySize / 8))
#在图片上添加数字3
draw = ImageDraw.Draw(image)
draw.text((0.9*xSize, 0.1*ySize),"3",(255,0,0),font)
#根据输入的命名来设置新图片名字,默认设置为“原名_new”,格式与原图相同
form = location.split('.')[1]
if target == "":
new_location = location.split('.')[0]
image.save(new_location+'_new.'+form)
else:
image.save(target+'.'+form)
关于ImageDraw模块的详细内容可以点击http://effbot.org/imagingbook/imagedraw.htm查看
而PIL模块的安装可以点击http://pillow-cn.readthedocs.org/zh_CN/latest/installation.html查看