当前位置:网站首页>matplotlib教程05---操作图像
matplotlib教程05---操作图像
2022-04-23 15:50:00 【KX-Lau】
欢迎关注公众号【Python开发实战】, 获取更多内容!
工具-matplotlib
使用matplotlib可以绘制出漂亮的图形。
在matplotlib中读取、生成和打印图像都非常简单。
保存图像
导入matplotlib
import matplotlib
import matplotlib.pyplot as plt
将图形保存到磁盘非常简单,只需要调用savefig()函数,并传递图形保存的名称即可。可用的图形格式取决于使用的图形后端。
plt.plot(x, x**2)
plt.savefig('my_square_function.png', transparent=True)

读取图像
只需要导入matplotlib.image模块,并调用imread()函数,并将文件名作为参数进行传递,图像数据会以numpy数组的形式返回。现在读取一下之前保存的平方函数。
import matplotlib.image as mpimg
img = mpimg.imread('my_square_function.png')
print(img.shape, img.dtype)
(288, 432, 4) float32
上面的结果显示加载的图像是一个288×432的,每个像素由一个四元素数组表示:红色、绿色、蓝色和alpha,存储为0到1之间的32位浮点数。现在调用imshow()函数。
plt.imshow(img)
plt.show()

显示图像时,隐藏坐标轴显示会更好一些。
plt.imshow(img)
plt.axis('off')
plt.show()

生成图像
生成一个图像也很容易。
import numpy as np
img = np.arange(100*100).reshape(100, 100)
print(img)
plt.imshow(img)
plt.show()
[[ 0 1 2 ... 97 98 99]
[ 100 101 102 ... 197 198 199]
[ 200 201 202 ... 297 298 299]
...
[9700 9701 9702 ... 9797 9798 9799]
[9800 9801 9802 ... 9897 9898 9899]
[9900 9901 9902 ... 9997 9998 9999]]

由于没有提供RGB级别,imshow()函数会自动将值映射到颜色渐变,默认情况下,颜色渐变从蓝色(低值)变为红色(高值),但可以选择其他颜色。
plt.imshow(img, cmap='hot')
plt.show()

也可以直接生成一个RGB图像。
img = np.empty((20, 30, 3))
img[:, :10] = [0, 0, 0.6]
img[:, 10:20] = [1, 1, 1]
img[:, 20:] = [0.6, 0, 0]
plt.imshow(img)
plt.show()

由于img数组非常小(20×30),当imshow()函数显示时,会将图像增大到figure的大小。拉伸原始图像,在原始像素之间会留下空白。默认情况下,imshow()函数会使用最近的非空白像素的颜色为每个空白像素着色。这样会产生像素化的图像。还可以使用不同的插值方法,如双线性插值来填补空白像素,但这会导致边缘模糊,可能在某些情况下会更好。
plt.imshow(img, interpolation='bilinear')
plt.show()

版权声明
本文为[KX-Lau]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_38727995/article/details/124352898
边栏推荐
- Go并发和通道
- Pytorch中named_parameters、named_children、named_modules函数
- Use bitnami PostgreSQL docker image to quickly set up stream replication clusters
- Metalife established a strategic partnership with ESTV and appointed its CEO Eric Yoon as a consultant
- Redis master-slave replication process
- shell_2
- Upgrade MySQL 5.1 to 5.611
- 多级缓存使用
- 新动态:SmartMesh和MeshBox的合作新动向
- Config learning notes component
猜你喜欢

ICE -- 源码分析

C language --- string + memory function

时序模型:长短期记忆网络(LSTM)
![[AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention](/img/bf/2b4914276ec1083df697383fec8f22.png)
[AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention

Application of Bloom filter in 100 million flow e-commerce system

New developments: new trends in cooperation between smartmesh and meshbox

5分钟,把你的Excel变成在线数据库,神奇的魔方网表excel数据库

贫困的无网地区怎么有钱建设网络?

MySQL集群模式與應用場景

腾讯Offer已拿,这99道算法高频面试题别漏了,80%都败在算法上
随机推荐
ICE -- 源码分析
编译,连接 -- 笔记
CAP定理
Accumulation of applet knowledge points
pgpool-II 4.3 中文手册 - 入门教程
[open source tool sharing] MCU debugging assistant (oscillograph / modification / log) - linkscope
Redis master-slave replication process
leetcode-374 猜数字大小
单体架构系统重新架构
Upgrade MySQL 5.1 to 5.611
时序模型:长短期记忆网络(LSTM)
JS regular détermine si le nom de domaine ou le chemin de port IP est correct
Multi level cache usage
Go语言数组,指针,结构体
Modèle de Cluster MySQL et scénario d'application
[section 5 if and for]
Pgpool II 4.3 Chinese Manual - introductory tutorial
PS add texture to picture
Go语言切片,范围,集合
Upgrade MySQL 5.1 to 5.69