ubuntu16.04+python PIL 中文标签

ubuntu16.04创建自定义中文字体库

由于需要中文标签,实用PIL加载自己的字体库。


1、下载保存字体库ttf文件


先从本机 C:\Windows\Fonts 拷贝或者网络上下载你想要安装的字体文件(*.ttf文件)到/usr/share/fonts/windows/ 目录下(如果系统中没有此目录,则自行自定义mkdir创建,如果是ttc文件,请改后缀为ttf)

2、修改字体文件的权限


#cd /usr/share/fonts/windows

#chmod 755 *.ttf

3、建立字体缓存


#sudo mkfontscale (如果提示 mkfontscale: command notfound,则需要安装# sudo apt-get installttf-mscorefonts-installer)
# sudo mkfontdir 

#sudo fc-cache -fv (如果提示 fc-cache: command not found,则需要安装# sudo apt-get install fontconfig)


最开始使用simsunb.ttf字体库还是没法支持中文,有重新安装了STFANGSO.TTF

通过fc-cache-fv 刷新字体库,可以看到我们的/usr/share/fonts/windows里面安装了2个字体库,如下


4、测试

代码如下:

from PIL import Image,ImageDraw,ImageFont

ttfont=ImageFont.truetype('/usr/share/fonts/windows/STFANGSO.TTF',40)

img = Image.open('test1.jpg')
draw = ImageDraw.Draw(img)
draw.text((40,40),'Ubuntu中文标签',fill=(0,0,0),font=ttfont)
img.show()

结果如下:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章