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

相关文章

Python中格式化format()方法详解

 Python中格式化format()方法详解 Python中格式化输出字符串使用format()函数, 字符串即类, 可以使用方法; Python是完全面向对象的语言, 任...

python实现sublime3的less编译插件示例

利用http://tool.oschina.net/less 提供的接口,发送请求进行远程编译.再将编译好的less,保存为同名后缀为css的文件中.第一次使用python,代码也是拼拼...

pymysql 开启调试模式的实现

pymysql 开启调试模式的实现

今天在排查线上一个奇怪的数据库连接问题,所以打开了 pymysql 的源码在阅读,发现 pymysql 在其 connections 模块里内置了一个 DEBUG 变量用于控制是否开启调...

python字符串加密解密的三种方法分享(base64 win32com)

1. 最简单的方法是用base64: 复制代码 代码如下:import base64 s1 = base64.encodestring('hello world')s2 = base64...

django项目用higcharts统计最近七天文章点击量

django项目用higcharts统计最近七天文章点击量

下载higcharts插件放在static文件夹下 前端引入 <script src="/static/highcharts/highcharts.js"></sc...