python图像处理模块Pillow的学习详解


Posted in Python onOctober 09, 2019

今天抽空学习了一下之前了解过的pillow库,以前看到的记得这个库可以给图片上加文字加数字,还可以将图片转化成字符画,不过一直没有找时间去学习一下这个模块,由于放假不用训练,所以就瞎搞了一下

0、工欲善其事,必先利其器

关于pillow库的安装有几种方式

0、使用pip安装

$ pip install pillow

1、使用easy_install

$ easy_install pillow

2、通过pycharm安装

1、学习并使用pillow库

#导入模块
from PIL import Image
#读取文件
img = Image.open('test.jpg')
#保存文件
#img.save(filename,format)
img.save(filename,"JPEG")
#获取图片大小
(width,height) = img.size
#获取图片的源格式
img_format = img.format
#获取图片模式,有三种模式:L(灰度图像),RGB(真彩色)和CMYK(pre-press图像)
img_mode = img.mode
#图片模式的转换
img = img.convert("L") #转化成灰度图像
#获取每个坐标的像素点的RGB值
r,g,b = img.getpixel((j,i))
#重设图片大小
img = img.resize(width,height)
#创建缩略图
img.thumbnail(size)

2、实战演练

其实应该很容易想到,如果要达到这种效果,应该能想得到就是获取图上每一点的RGB值,然后根据这三种值确定这一点采用什么字符,其实根据RGB来确定的交灰值,所以可以将图片转化成灰度图片,来直接获取每一点的灰度,或者通过灰度的转换公式来使得RGB三值转化成灰度

#coding:utf-8
from PIL import Image
#要索引的字符列表
ascii_char = list("$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ")
length = len(ascii_char)
img = Image.open('03.jpg')  #读取图像文件
(width,height) = img.size
img = img.resize((int(width*0.9),int(height*0.5))) #对图像进行一定缩小
print(img.size)
def convert(img):
 img = img.convert("L") # 转为灰度图像
 txt = ""
 for i in range(img.size[1]):
  for j in range(img.size[0]):
   gray = img.getpixel((j, i))  # 获取每个坐标像素点的灰度
   unit = 256.0 / length
   txt += ascii_char[int(gray / unit)] #获取对应坐标的字符值
  txt += '\n'
 return txt

def convert1(img):
 txt = ""
 for i in range(img.size[1]):
  for j in range(img.size[0]):
   r,g,b = img.getpixel((j, i))   #获取每个坐标像素点的rgb值
   gray = int(r * 0.299 + g * 0.587 + b * 0.114) #通过灰度转换公式获取灰度
   unit = (256.0+1)/length
   txt += ascii_char[int(gray / unit)] # 获取对应坐标的字符值
  txt += '\n'
 return txt

txt = convert(img)
f = open("03_convert.txt","w")
f.write(txt)   #存储到文件中
f.close()

给图片加上文字(福利预警,前方有福利!!!!)

#coding:utf-8
from PIL import Image,ImageDraw,ImageFont

#http://font.chinaz.com/zhongwenziti.html 字体下载网站

img = Image.open('PDD01.jpg')
draw = ImageDraw.Draw(img)
myfont = ImageFont.truetype('HYLiuZiHeiJ.ttf',size=80)
fillcolor = 'pink'
(width, height) = img.size
#第一个参数是加入字体的坐标
#第二个参数是文字内容
#第三个参数是字体格式
#第四个参数是字体颜色
draw.text((40,100),u'萌萌哒',font=myfont,fill=fillcolor)
img.save('modfiy_pdd01.jpg','jpeg')

给图片加上数字

这个大家应该见过的,就是有些头像的左上角的那个小红圈加上白色的数字,其实方法和上面那个加文字的差不多 

讲道理,我还不如用ps,移坐标移到要死要死的

#coding:utf-8
from PIL import Image,ImageDraw,ImageFont
img = Image.open("03.jpg")
draw = ImageDraw.Draw(img)
myfont = ImageFont.truetype(u"时光体.ttf",50)
(width,height) = img.size
draw.ellipse((width-40,0,width,40),fill="red",outline="red") #在图上画一个圆
draw.text((width-30,-8),'1',font=myfont,fill='white')
img.save('03_modify.jpg')

生成4位随机验证码

#coding:utf-8
from PIL import Image,ImageDraw,ImageFont,ImageFilter
import random
"""
创建四位数的验证码
"""
#产生随机验证码内容
def rndTxt():
 txt = []
 txt.append(random.randint(97,123))  #大写字母
 txt.append(random.randint(65,90))  #小写字母
 txt.append(random.randint(48,57))  #数字
 return chr(txt[random.randint(0,2)])

#随机颜色(背景)
def rndColor1():
 return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255))

#随机颜色(字体)
def rndColor2():
 return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127))

#240x60:
width = 60*4
height = 60
img = Image.new('RGB',(width,height),(255,255,255))
font = ImageFont.truetype(u'时光体.ttf',36)
draw = ImageDraw.Draw(img)
#填充每个像素
for x in range(width):
 for y in range(height):
  draw.point((x,y),fill=rndColor1())

#输出文字
for txt in range(4):
 draw.text((60*txt+10,10),rndTxt(),font=font,fill=rndColor2())
#模糊化处理
#img = img.filter(ImageFilter.BLUR)
img.save("code.jpg")

学习于:廖雪峰的官方网站

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python 动态获取当前运行的类名和函数名的方法
Apr 15 Python
Python爬取Coursera课程资源的详细过程
Nov 04 Python
Python获取当前路径实现代码
May 08 Python
Python基于Socket实现的简单聊天程序示例
Aug 05 Python
pycharm重置设置,恢复默认设置的方法
Oct 22 Python
Django添加KindEditor富文本编辑器的使用
Oct 24 Python
利用Pyhton中的requests包进行网页访问测试的方法
Dec 26 Python
我就是这样学习Python中的列表
Jun 02 Python
Python参数类型以及常见的坑详解
Jul 08 Python
基于python进行抽样分布描述及实践详解
Sep 02 Python
python读取ini配置的类封装代码实例
Jan 08 Python
详解查看Python解释器路径的两种方式
Oct 15 Python
Python 中pandas索引切片读取数据缺失数据处理问题
Oct 09 #Python
详解python路径拼接os.path.join()函数的用法
Oct 09 #Python
Django Docker容器化部署之Django-Docker本地部署
Oct 09 #Python
Python3实现zip分卷压缩过程解析
Oct 09 #Python
基于Python新建用户并产生随机密码过程解析
Oct 08 #Python
Python小程序 控制鼠标循环点击代码实例
Oct 08 #Python
Python3 无重复字符的最长子串的实现
Oct 08 #Python
You might like
第三节--定义一个类
2006/11/16 PHP
PHP中通过加号合并数组的一个简单方法分享
2011/01/27 PHP
PHP之数组学习
2011/05/29 PHP
php中用加号与用array_merge合并数组的区别深入分析
2013/06/03 PHP
CI映射(加载)数据到view层的方法
2016/03/28 PHP
xtree.js 代码
2007/03/13 Javascript
javascript下过滤数组重复值的代码
2007/09/10 Javascript
jQuery实现仿Google首页拖动效果的方法
2015/05/04 Javascript
Ionic实现仿通讯录点击滑动及$ionicscrolldelegate使用分析
2016/01/18 Javascript
详解jQuery中的DOM操作
2016/12/23 Javascript
js定时器+简单的动画效果实例
2017/11/10 Javascript
NodeJS爬虫实例之糗事百科
2017/12/14 NodeJs
Javascript中弹窗confirm与prompt的区别
2018/10/26 Javascript
Vue动态加载异步组件的方法
2018/11/21 Javascript
JS为什么说async/await是generator的语法糖详解
2019/07/11 Javascript
jquery轮播图插件使用方法详解
2020/07/31 jQuery
实例说明Python中比较运算符的使用
2015/05/13 Python
Python使用Supervisor来管理进程的方法
2015/05/28 Python
python使用Image处理图片常用技巧分析
2015/06/01 Python
python opencv检测目标颜色的实例讲解
2018/04/02 Python
详解如何为eclipse安装合适版本的python插件pydev
2018/11/04 Python
Python OpenCV对本地视频文件进行分帧保存的实例
2019/01/08 Python
Python基本socket通信控制操作示例
2019/01/30 Python
Django实现web端tailf日志文件功能及实例详解
2019/07/28 Python
pytorch如何冻结某层参数的实现
2020/01/10 Python
浅谈Pytorch torch.optim优化器个性化的使用
2020/02/20 Python
Kathmandu美国网站:新西兰户外运动品牌
2019/03/23 全球购物
Visual-Click葡萄牙:欧洲领先的在线眼镜商
2020/02/17 全球购物
综合办公室个人的自我评价
2013/12/22 职场文书
国际会议邀请函范文
2014/01/16 职场文书
优秀体育委员自荐书
2014/01/31 职场文书
大学四年个人的自我评价
2014/02/26 职场文书
党的群众路线对照检查材料
2014/09/22 职场文书
2014基建处领导班子“四风”对照检查材料思想汇报
2014/10/04 职场文书
工作总结之小学教师体育工作范文(3篇)
2019/10/07 职场文书
德劲DE1108畅想
2021/04/22 无线电