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库来做 ...
随机推荐
- Git 项目上传至github入门实战并解决常见错误
1.Git GUI 首先,在push到github的项目必须先建立版本(即creat repository的名字一样),一般是先pull下来,再push(为了防止有其他人提交了代码,而你却不知道,造 ...
- solaris11-text-安装GUI(gnome)
http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=45057&id=3018467 1.下载所需的资源Text Ins ...
- Oracle数据库软件标准版的一个限制:仅仅能用一个rman channel
Oracle数据库软件标准版的一个限制:仅仅能用一个rman channel Restrictions in "Standard Edition" Rman channel all ...
- 《黑马程序猿》 cocos2d游戏引擎复习笔记一
/** ----------------------------游戏场景的搭建-------------------------------- 1首先创建一个surfaceview ,它能够在子线程中 ...
- ubuntu下eclipse连接mysql
提示:一定要保证电脑处于联网状态 我们要下载一个mysql-connector-java-5.0.8-bin.jar的东西(当然这个jar包的版本号和你的mysql版本号的关系不是非常大),放到你新建 ...
- HDU 5536/ 2015长春区域 J.Chip Factory Trie
Chip Factory Problem Description John is a manager of a CPU chip factory, the factory produces lots ...
- 【字符串处理】AC自动机知识点&代码
代码: #include<iostream> #include<vector> #include<cstdio> #include<queue> #in ...
- n阶导函数存在与n阶可导的区别
1.f(x)n阶导函数存在 <=======> f(n)(x)存在 指的是在某个区间内有定义 2.f(x)n阶可导根据题意可以有两种不同的解释: ①.题目中说的是在某点即在x=x0处n ...
- ConcurrentDictionary中的 TryRemove
class A { internal int value; } ConcurrentDictionary<int, A> dic = new ConcurrentDictionary< ...
- 【转】iPhone获取状态栏和导航栏尺寸(宽度和高度)
原文网址:http://blog.csdn.net/chadeltu/article/details/42708605 iPhone开发当中,有时需要获取状态栏和导航栏高度.宽度信息,方便布局其他控件 ...