图像处理PILLOW的使用
1.安装
pip install Pillow
2.使用
1)图片缩放
from PIL import Image
im = Image.open('dog.jpg')
w,h = im.size #获取图像的尺寸
im.thumbnail((w/2,h/2)) #将图像缩放50%
#im.show() #显示图片
im.save('dog_thumbnail.jpg') #保存图片
效果图:


2.图片模糊
im = Image.open('dog.jpg')
im2 = im.filter(ImageFilter.BLUR)
im2.save('dog_blur.jpg')
效果图:

3.生成验证码
from PIL import ImageDraw,ImageFont,ImageFilter
import random #返回一个A-Z的随机字母
def randomChar():
return chr(random.randint(65,90)) #随机颜色
def randomColor1():
return (random.randint(64,255),random.randint(64,255),random.randint(64,255)) #随机颜色2
def randomColor2():
return (random.randint(32,127),random.randint(32,127),random.randint(32,127)) width = 60 * 4
height = 60
#创建白色图片
image = Image.new('RGB',(width,height),(255,255,255))
#创建font对象
#OSError: cannot open resource 需要指定字体库的系统路径
#font = ImageFont.truetype("Arial.ttf",36)
font = ImageFont.truetype("C:/windows/fonts/Arial.ttf",36)
#创建Draw对象,用于向白色背景图片上绘图
draw = ImageDraw.Draw(image)
#填充每一个像素
for x in range(width):
for y in range(height):
draw.point((x,y),fill=randomColor1()) #输出文字
for i in range(4):
draw.text((60*i+10,10),randomChar(),font=font,fill=randomColor2()) #模糊
image = image.filter(ImageFilter.BLUR)
image.save("captcha.jpg")
效果:

4.裁图,旋转,粘贴
from PIL import Image
im = Image.open('dog.jpg')
box = (100,50,350,250) #要裁剪的矩形区域,坐标以左上角为原点
region = im.crop(box) #返回裁剪到的图片
#region.show()
region = region.transpose(Image.ROTATE_180) #对图像进行旋转
im.paste(region,box) #将旋转后的图像粘回原图
im.show()
效果图:

5.添加图片水印:
from PIL import Image
image = Image.open('dog.jpg')
logo = Image.open('logo.png')
logoW,logoH= logo.size
imageW,imageH = image.size
image.paste(logo,(imageW - logoW, imageH - logoH))
image.show()
效果:

6.添加透明文字水印:
参考: http://pythoncentral.io/watermark-images-python-2x/
def add_watermark(in_file, text,font,out_file='watermark.jpg', angle=23, opacity=0.25):
'''
:param in_file: 要添加水印的图片
:param text: 文字水印内容
:param out_file: 添加水印后的图片
:param font:水印字体
:param angle: 水印旋转角度
:param opacity: 水印透明度
'''
img = Image.open(in_file).convert('RGB')
watermark = Image.new('RGBA', img.size, (0, 0, 0, 0))
size = 2
n_font = ImageFont.truetype(font, size)
#getsize返回水印文字对应字体大小的宽度和高度
n_width, n_height = n_font.getsize(text)
#找到使得水印文字宽度最接近图片宽度的字体大小
while n_width + n_height < watermark.size[0]:
size += 2
n_font = ImageFont.truetype(font, size)
n_width, n_height = n_font.getsize(text)
draw = ImageDraw.Draw(watermark, 'RGBA')
draw.text(((watermark.size[0] - n_width) / 2,
(watermark.size[1] - n_height) / 2),
text, font=n_font)
watermark = watermark.rotate(angle, Image.BICUBIC)
alpha = watermark.split()[3]
#通过降低亮度和对比度来降低水印的透明度
alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
#设置透明度
watermark.putalpha(alpha)
#添加水印
Image.composite(watermark, img, watermark).save(out_file, 'JPEG') FONT = 'C:/Windows/fonts/Arial.ttf'
add_watermark('dog.jpg','python',font=FONT)
效果图:

相关资料:
https://pillow.readthedocs.io/en/latest/handbook/tutorial.html
http://pillow-cn.readthedocs.io/zh_CN/latest/handbook/tutorial.html
图像处理PILLOW的使用的更多相关文章
- 杂项之图像处理pillow
杂项之图像处理pillow 本节内容 参考文献 生成验证码源码 一些小例子 1. 参考文献 http://pillow-cn.readthedocs.io/zh_CN/latest/ pillow中文 ...
- 第二篇:杂项之图像处理pillow
杂项之图像处理pillow 杂项之图像处理pillow 本节内容 参考文献 生成验证码源码 一些小例子 1. 参考文献 http://pillow-cn.readthedocs.io/zh_CN/ ...
- 图像处理pillow模块
pillow模块: -->基本的图像处理模块 Pip install pillow from PIL import Image #1.读取图片 im = Image.open('/test.jp ...
- 潭州课堂25班:Ph201805201 爬虫基础 第九课 图像处理- PIL (课堂笔记)
Python图像处理-Pillow 简介 Python传统的图像处理库PIL(Python Imaging Library ),可以说基本上是Python处理图像的标准库,功能强大,使用简单. 但是由 ...
- python爬虫基础15-python图像处理,PIL库
Python图像处理-Pillow 简介 Python传统的图像处理库PIL(Python Imaging Library ),可以说基本上是Python处理图像的标准库,功能强大,使用简单. 但是由 ...
- 6 python高级数据处理和可视化
6.2. pyplot作图 1.折线图和散点图 t = np.arange(0,4,0.1) plt.plot(t,t,'o',t,t+2,t,t**2,'o') plt.show() 2.柱线图 p ...
- python武器库
1,开发库You-GetYouTube/Youku/Niconico视频下载器Zipline一个Pythonic的交易算法库docopt为Python程序创造一个优雅的命令行界面PDFMinerPyt ...
- Python图像处理库:Pillow 初级教程
Python图像处理库:Pillow 初级教程 2014-09-14 翻译 http://pillow.readthedocs.org/en/latest/handbook/tutorial.html ...
- 图像识别的前期工作——使用pillow进行图像处理
pillow是个很好用的python图像处理库,可以到官方网站下载最新的文件.如果官网的任何PIL版本都不能与自己的python版本对应,或安装成功后发现运行出错,可以尝试从一个非官方的whl网站下载 ...
随机推荐
- 测试那些事儿—简述CPU的工作原理
简单介绍CPU的工作原理 1.内部架构 CPU是由晶体管组成,其根本任务就是执行指令和数据处理,对计算机来说,就是由0和1组成的序列.CPU从逻辑上可分为3个模块,分别是控制单元,运算单元和存储单元. ...
- js 调用 手机 相机摄像机麦克风
https://www.cnblogs.com/avon/p/5996368.html
- PWA需要的技术
1 Manifest https://developer.mozilla.org/zh-CN/docs/Web/Manifest 2 Service Work ...
- LSOF查看linux中文件打开情况
如何查看linux中文件打开情况 前言 我们都知道,在linux下,“一切皆文件”,因此有时候查看文件的打开情况,就显得格外重要,而这里有一个命令能够在这件事上很好的帮助我们-它就是lsof. lin ...
- LA5009 Error Curves
题意 PDF 分析 因为这些函数都可以看成下凸的,所以总函数也是下凸的(可用反证法证明). 三分答案即可,时间复杂度\(O(100)\) 代码 #include<bits/stdc++.h> ...
- Mybaits代码生成使用
https://jingyan.baidu.com/album/00a07f3869881082d028dc98.html PS:根据数据库中的表 生成代码
- vue 2.0 vue.set的使用方法
这里我定义了一个列表数据,我将通过三个不同的按钮来控制列表数据. 首先在列表中动态新增一条数据: <!DOCTYPE html><html><head lang=&quo ...
- 02Linux环境配置
Linux环境配置 修改ip地址 1,图形化界面 2,setup 命令虚拟界面 3,修改配置文件(以网络方式为NAT示例) vi /etc/sysconfig/network-scripts/ifcf ...
- 晒一晒Jenkins那些常用插件
Jenkins插件大师 作为CI/CD的调度中心,Jenkins具有十八般武艺,目前已有1700多个插件,功能强大到似乎有点过分了.本文主要列出平时我们常用的插件. 以下这两个网站是Jenkins ...
- firefox extension教程
https://developer.mozilla.org/zh-CN/docs/Add-ons/Overlay_Extensions/XUL_School/The_Essentials_of_an_ ...