Python PIL图片添加字体的例子

yipeiwu_com6年前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设计】。

相关文章

详解从Django Allauth中进行登录改造小结

大概来介绍一下 Django Allauth 改造的期间遇到的一些问题和改造方法,在此之前我只想说——Django Allauth 是屑。 为什么我说 Django Allauth 是屑...

Python基础教程之tcp socket编程详解及简单实例

Python tcp socket编程详解 初学脚本语言Python,测试可用的tcp通讯程序: 服务器: #!/usr/bin/env python # -*- coding:...

解决Python内层for循环如何break出外层的循环的问题

偶然发现了for…else…这种用法,使用这个实现了break跳出嵌套的for循环 In [31]: for i in range(1,5): ...: for j in r...

python使用datetime模块计算各种时间间隔的方法

本文实例讲述了python使用datetime模块计算各种时间间隔的方法。分享给大家供大家参考。具体分析如下: python中通过datetime模块可以很方便的计算两个时间的差,dat...

python http基本验证方法

如下所示: #!usr/bin/env python # -*- coding: utf-8 -*- import urllib2 LOGIN = "" PASSWORD = "...