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中的localtime()方法使用详解

 localtime()方法类似gmtime()方法,但它转换秒数为本地时间。如果不设置秒时或None,所返回的当前时间使用time()方法。在dst标志被设置为1时,夏令时适...

python对视频画框标记后保存的方法

需要画框取消注释rectangle import cv2 import os,sys,shutil import numpy as np # Open the input mov...

Python 中PyQt5 点击主窗口弹出另一个窗口的实现方法

Python 中PyQt5 点击主窗口弹出另一个窗口的实现方法

1.先使用Qt designer设计两个窗口,一个是主窗口,一个是子窗口    ...

python实现将json多行数据传入到mysql中使用

将json多行数据传入到mysql中使用python实现 表需要提前创建,字符集utf8 如果不行换成utf8mb4 import json import pymysql def...

python通过文件头判断文件类型

对于提供上传的服务器,需要对上传的文件进行过滤。 本文为大家提供了python通过文件头判断文件类型的方法,避免不必要的麻烦。 分享代码如下 import struct # 支...