Recently due to work needs, need to add numbers on the image, query the data, wrote a method, and tested, because the code used in the PIL library, need to download the installation,: http://www.pythonware.com/products/pil/, Download imaging-1.1.7.tar.gz after extracting, Imaging-1.1.7, run setup.py under command line to install
The specific implementation code is as follows:
#-*-Coding:utf-8-*-
Import PIL
From PIL import Imagefont
From PIL import Image
From PIL import Imagedraw
"' Add a number to the picture, imagefile the picture file to add the number, fontfile the font file,
Targetimagefile a picture file to be saved after adding a number, txtnum the added number "
def drawimagetxt (Imagefile,fontfile,targetimagefile,txtnum):
#设置字体大小
Font = Imagefont.truetype (Fontfile, 46)
#打开文件
im = Image.open (imagefile)
#字体坐标
x = IM.SIZE[0]/2
y = IM.SIZE[1]/2
p = (x, y)
Draw = Imagedraw.draw (IM)
Draw.text (P, Txtnum, (255,255,255), Font=font)
#保存
Im.save (Targetimagefile)
#关闭
Im.close ()
#测试代码
ImageFile = r "D:\test.png"
Fontfile = r "D:\arialuni.ttf"
Targetimagefile = r "D:\target.jpg"
Txtnum= ' 34 '
Drawimagetxt (Imagefile,fontfile,targetimagefile,txtnum)
Add a digital source code to a Python image