python PIL图像处理-图片上添加文字
首先需要安装库pillow
cmd安装命令:pip install pillow

安装完后,编写脚本如下:
from PIL import Image, ImageDraw, ImageFont def gen_img(size=None):
if size is None:
size = 400
#生成大小为400x400RGBA是四通道图像,RGB表示R,G,B三通道,A表示Alpha的色彩空間
image = Image.new(mode='RGBA', size=(400, 400), color=(255, 55, 55))
# ImageDraw.Draw 简单平面绘图
draw_table = ImageDraw.Draw(im=image)
# 直接显示图片
image.show() def pic_open(filepath):
#图片打开与显示
image = Image.open(filepath)
return image def get_size(image):
#获取图像的宽和高
width, height = image.size
return width,heitht def pic_text(filepath,size,text,setFont,fillColor,filename,direction=None):
print(filepath,size,text,setFont,fillColor)
#打开图片
image=pic_open(filepath)
#新建绘图对象
draw = ImageDraw.Draw(image)
#显示图片
image.show()
draw.text((40,40),text,font=setFont,fill=fillColor,direction=None)
image.show()
#保存
pic_save(image,filename) def pic_save(image,filename):
#保存
image.save(filename) if __name__=="__main__": size=None
#gen_img() #** ImageFont模块**
#选择文字字体和大小
setFont = ImageFont.truetype('C:/windows/fonts/Dengl.ttf', 20)
#设置文字颜色
fillColor = "#0000ff" #蓝色
text="兔子等着瞧"
size=(40,40)
filepath="F:/temp/red.png"
filename="F:/temp/redsave.png" #打开图片
image=pic_open(filepath)
#添加文字
pic_text(filepath,size,text,setFont,fillColor,filename,direction=None)
首先是打开一个图片

添加文字后图片

python PIL图像处理-图片上添加文字的更多相关文章
- python如何在图片上添加文字(中文和英文)
Python在图片上添加文字的两种方法:OpenCV和PIL 一.OpenCV方法 1.安装cv2 pip install opencv-python 2.利用putText方法来实现在图片的指定位置 ...
- 用python, PIL在图像上添加文字(可以控制,调节为水印等)
最近想在图像上,添加想要的文字,首先想到的是matplotlib,但是这个更加倾向于画图(柱状图,折线图之类) opencv这个库肯定也行,但是为了和我现有程序连接在一起,我选择了PIL 其中字体的设 ...
- python 图片上添加文字
import PIL from PIL import ImageFont from PIL import Image from PIL import ImageDraw #设置字体,如果没有,也可以不 ...
- Office WORD如何在图片上添加文字
如图所示,在图片格式中选择图片衬于文字下方即可,这样看起来感觉就像在图片上直接加字一样,没有生硬的感觉. 最终效果: Word如何在图片上添加文字Word如何在图片上添加文字Word如何在图片上添加文 ...
- C#在图片上添加文字代码
创建.NET WinForm程序,设置项目的默认命名空间为Keleyi.Com,在窗体上添加一个PictureBox控件pictureBox_keleyi_com和一个Button控件button_A ...
- java在图片上添加文字
业务需求要在图片上添加水印.下面粘出代码供自己和大家分享 package com.pro.drawTextOnImg; import java.awt.Color; import java.awt.F ...
- html+css 在图片上添加文字
html <view class="container"> <image class="" src="{{book.image}}& ...
- python 图片上添加数字源代码
最近因工作需要,需要在图片上添加数字,查询了资料,自己写了一个方法,并进行了测试,由于代码用到了PIL库,需要下载安装,下载地址:http://www.pythonware.com/products/ ...
- 图像处理---《在图片上打印文字 windows+GDI+TrueType字体》
图像处理---<在图片上打印文字 windows+GDI+TrueType字体> 刚开始使用的是putText()函数做,缺陷是只能显示非中文: 接着,看大多数推荐Freetype库来做 ...
随机推荐
- django 和 mysql的一次troubleshooting
下面是一次用django连接mysql的经历,记录下来也许以后会有帮助. 首先是用django的./manage.py syncdb 去连接mysql -bash-3.2$ ./manage.py s ...
- [Cypress] Test Variations of a Feature in Cypress with a data-driven Test
Many applications have features that can be used with slight variations. Instead of maintaining mult ...
- BasePath问题-nginx负载均衡配置
在配置nginx+tomcat好后.将项目加入到webapps中.发现訪问主页时,css与js訪问不到,导致主页布局出错.细致分析原因后发现css与js的地址是basePath得出的.而basePat ...
- java 将byte[]转为各种进制的字符串
public void test() { byte[] bytes = new byte[10000000]; for (int i = 0; i < 10000000; i++) { if ( ...
- Android5.0 Recovery源代码分析与定制(一)【转】
本文转载自:http://blog.csdn.net/morixinguan/article/details/72858346 版权声明:本文为博主原创文章,如有需要,请注明转载地址:http://b ...
- Spark入门之DataFrame/DataSet
目录 Part I. Gentle Overview of Big Data and Spark Overview 1.基本架构 2.基本概念 3.例子(可跳过) Spark工具箱 1.Dataset ...
- A. Jeff and Digits(cf)
A. Jeff and Digits time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- LocalDateTime查找最近的五分钟点
/** * 最近的五分钟 * @param dateTime * @return */ public static LocalDateTime getNear5(LocalDateTime dateT ...
- java 实现yaml 数据转json与map
首先引入snakeyaml-1.16.jar的包. 直接上代码: package com.ming.yaml; import java.util.Map; import org.yaml.snakey ...
- Lambda&Linq
var list = new List<Model> { , UserName = ", Email = "zhang3@yoy.com"}, , UserN ...