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迭代器过程详解

分析 我们都知道一个可迭代对象可以通过iter()可以返回一个迭代器。 如果想要一个对象称为可迭代对象,即可以使用for,那么必须实现__iter __()方法。 在一个类...

详解在Python程序中自定义异常的方法

通过创建一个新的异常类,程序可以命名它们自己的异常。异常应该是典型的继承自Exception类,通过直接或间接的方式。 以下为与RuntimeError相关的实例,实例中创建了一个类,基...

django-rest-swagger的优化使用方法

如下所示: requirements.txt django==1.10.5 djangorestframework==3.5.3 django-rest-swagger==2.1...

TensorFlow中权重的随机初始化的方法

一开始没看懂stddev是什么参数,找了一下,在tensorflow/python/ops里有random_ops,其中是这么写的: def random_normal(shape,...

python实现控制电脑鼠标和键盘,登录QQ的方法示例

本文实例讲述了python实现控制电脑鼠标和键盘,登录QQ的方法。分享给大家供大家参考,具体如下: import os from pynput.mouse import Button...