Python PIL图片添加字体的例子

yipeiwu_com5年前Python基础

效果

左边原图,右面添加字体后保存的图。

代码

# -*- coding: utf-8 -*-
import PIL.Image as Image
import PIL.ImageColor as ImageColor
import PIL.ImageDraw as ImageDraw
import PIL.ImageFont as ImageFont
"""
  author@:xuna
  python2.7
"""

#设置字体(LiberationSans-Regular.ttf这是我ubuntu16.04自带的字体)
font = ImageFont.truetype('LiberationSans-Regular.ttf', 60)

#打开图片
imageFile = "/home/xuna/桌面/笔记/1.jpg"
im1=Image.open(imageFile)

# 在图片上添加文字 1
draw = ImageDraw.Draw(im1)

# (0,0):坐标 "XUNALOVE":添加的字体 (0,0,255):字体颜色 font:字体大小
draw.text((0, 0),"XUNALOVE",(0,0,255),font=font)
draw = ImageDraw.Draw(im1)

# 保存
im1.save("/home/xuna/桌面/笔记/res.png")

以上这篇Python PIL图片添加字体的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python获取邮件地址的方法

本文实例讲述了Python获取邮件地址的方法。分享给大家供大家参考。具体实现方法如下: import email.Utils   def getCleanMailAddress(st...

python文件读写并使用mysql批量插入示例分享(python操作mysql)

复制代码 代码如下:# -*- coding: utf-8 -*-'''Created on 2013年12月9日 @author: hhdys''' import osimport m...

python模拟鼠标拖动操作的方法

python模拟鼠标拖动操作的方法

本文实例讲述了python模拟鼠标拖动操作的方法。分享给大家供大家参考。具体如下: pdf中的书签只有页码,准备把现有书签拖到一个目录中,然后添加自己页签。重复的拖动工作实在无趣,还是让...

python: line=f.readlines()消除line中\n的方法

python: line=f.readlines()消除line中\n的方法

测试代码 #!/ust/bin/env python3 f = open("name.txt") date = f.readlines() print(date) f.close(...

Python获取指定字符前面的所有字符方法

在用C和python编程时遇到的一个问题是: 用网口发送过来1k数据,数据格式是json,但是发送时不知道需要的大小,因为不同任务大小不一样,所以统一发送1024字节,统一接收1024了...