首页 > 技术文章 > python 习题集

kele1997 2017-09-26 11:32 原文

python习题集

第一天在图像上加红色数字

2017-7-23


from PIL import Image,ImageDraw,ImageFont

def add_num(img):
	draw=ImageDraw.Draw(img)
	myfont=ImageFont.truetype('c:/windows/fonts/Arial.ttf',size=40)
	#arial.ttf是字体,size是字体大小
	fillcolor="#ff0000"
	width,height=img.size
	draw.text((width-40,0),'99',font=myfont,fill=fillcolor)
	img.save('result.jpg','jpeg')
	return 0

if __name__=='__main__':#这里的__name__和__main__是全局变量 
	image=Image.open('image.jpg')
	add_num(image)

坑:pentestbox中没有PIL库,python -m pip install PIL,提示更新pip,更新完之后,还是安装不上,python -m pip search PIL,确实发现有PIL库,但是却无法安装,google,提示缺少freetype库,但是pentestbox似乎不能安装,放弃。。。。
google得:pillow是PIL的双胞胎(手动滑稽),而且仍在更新维护,回到pentestbox,发现有pillow,于是正常使用即可.......,绕了一圈,from PIL import Image,ImageFont,ImageDraw

推荐阅读