这是我参与11月更文挑战的第22天,活动详情查看:2021最后一次更文挑战
前言
众所周知,matplotlib 模块非常强大,不仅提供绘制不同绘制折线、柱状、散点等图表pyplot,还提供常用图形绘制patches,绘制动态图类animation,具体详情可见往期文章。
-
matplotlib 底层结构:matplotlib模块底分为三层脚本、美工和后端功能学习
-
matplotlib 绘制折线图:matplotlib 模块提供pyplot.plot()绘制折线图方法
-
matplotlib 绘制柱状图:matplotlib 模块提供pyplot.bar()绘制柱状图方法
-
matplotlib pyplot绘制动态图:matplotlib 模块提供pyplot交互模式绘制动态图
-
matplotlib Animation类绘制动画: matplotylib 模块提供animation类绘制动态图
matplotlib 模块中也提供matplotlib.image类对图像加载、缩放和显示操作。
本期,我们对matplotlib.image类相关方法和属性进行学习,let's go~
1. matplotlib.image 概述
matplotlib.image 是专门提供对图像进行加载、缩放和展示的操作。
- matplotlib.image 将图像加载转换成三维数组,列表中每一组数据代表一个像素
- matplotlib.image 可以将生成或者导入的numpy数组渲染生成图像
- matplotlib.image 可以应用在对图像添加不同色域进行调色处理
- matplotlib.image 对图像添加clim来限定rgb,同时可添加标度来进行查看
- matplotlib.image 使用interpolation属性来对图像添加马赛克、虚化等处理
2. 图像处理相关方法
-
matplotlib.image 模块提供类方法
| 类方法 | 说明 |
|---|---|
| matplotlib.image._Image(ax) | Image抽象类,继承Artist |
| matplotlib.image.AxesImage(ax) | 为图片添加Axes对象,是_Image的子类之一 |
| matplotlib.image.PcolorImage(ax) | 通过使用不规则的网格制作pcolor样式绘图 |
| matplotlib.image.FigureImage(fig) | 将图片添加到画布上 |
| matplotlib.image.BboxImage(bbox) | 给定特定bbox的Image类 |
| matplotlib.image.NonUniformImage(ax) | 处理非均匀的图像Image类 |
-
matplotlib.image 模块提供方法
| 方法 | 作用 |
|---|---|
| matplotlib.image.imread(frame) | 加载图片 |
| matplotlib.image.imsave(frame,arr) | 保存图片文件 |
| matplotlib.image.thumbnail(infile,thumfile) | 对图片进行缩略处理 |
| matplotlib.image.pil_to_array(pilImage) | 加载PIL图片作为numpy int数组返回 |
| matplotlib.image.composite_images(images, renderer) | 将多个RGBA图像合成为一个 |
3. 图像处理步骤
matplotlib模块中,我们可以使用image类相关方法来对图像进行处理,主要有以下步骤:
- 导入绘制图形的matplotlib.pyplot和图像处理的matplotlib.image类
import matplotlib.pyplot as plt
import matplotlib.image as mpimage
- 调用pyplot.subplots()创建两个Axes对象
fig,(ax1,ax2) = plt.subplots(1,2,sharey=True)
- 调用matplotlib.image.imread(file) 加载图像生成三维数据
img = mpimage.imread("image.png")
- 对图像的三维数据进行调取图像像素通道只显示单通道数据
create_img = img[:,:,0]
- 调用pyplot.imshow(img) 将图像单通道三维数据转换成图像
ax1.imshow(img)
ax2.imshow(create_img)
- 调用pyplot.show() 将图像渲染到画布上
ax1.set_title("原图")
ax2.set_title("显示单通道")
pyplot.show()
- 原图对比显示单通道色系结果
- 使用pyplt.imshow() cmap属性对图像进行调色
create_img = img[:,:,2]
ax2.imshow(create_img,cmap="Blues_r")
- 调用pyplot.colorbar()显示颜色条
img = mpimage.imread("image.jpg")
create_img = img[:, :, 2]
fig = plt.figure()
ax1 = fig.add_subplot(1,2,1)
im = plt.imshow(img,cmap="hot")
im.set_clim(0.0,0.7)
ax1.set_title("原图")
plt.colorbar(orientation='horizontal')
ax2 = fig.add_subplot(1,2,2)
im = plt.imshow(create_img,cmap="Blues_r")
im.set_clim(0.0,0.7)
ax2.set_title("调色")
plt.colorbar(orientation="horizontal")
- 对图像进行缩放插值处理调用thumbnail()
from PIL import Image
Pimg = Image.open("BQ.jpg")
ax2 = fig.add_subplot(1,2,2)
Pimg.thumbnail((64, 64), Image.ANTIALIAS)
im = plt.imshow(Pimg)
im.set_clim(0.0,0.7)
ax2.set_title("插值")
plt.colorbar(orientation="horizontal")
- 对图像进行模糊处理可以再添加interpolation属性
Pimg = Image.open("BQ.jpg")
fig = plt.figure()
ax1 = fig.add_subplot(1,2,1)
Pimg.thumbnail((64, 64), Image.ANTIALIAS)
im = plt.imshow(Pimg,interpolation="bicubic")
im.set_clim(0.0,0.7)
ax1.set_title("bicubic")
plt.colorbar(orientation='horizontal')
ax2 = fig.add_subplot(1,2,2)
Pimg.thumbnail((64, 64), Image.ANTIALIAS)
im = plt.imshow(Pimg,interpolation="nearest")
im.set_clim(0.0,0.7)
ax2.set_title("nearest")
plt.colorbar(orientation="horizontal")
总结
本期,我们对matplotlib.image 对图像处理相关方法进行学习和掌握。在对图像进行模糊处理时,通常会与PIL图像库进行结合使用。
以上是本期内容,欢迎大佬们点赞评论,我们下期见~