当前位置:网站首页>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
边栏推荐
- 一刷312-简单重复set-剑指 Offer 03. 数组中重复的数字(e)
- utils. Deprecated in35 may be cancelled due to upgrade. What should I do
- Basic greedy summary
- MySQL optimistic lock to solve concurrency conflict
- Go language, array, pointer, structure
- Application of Bloom filter in 100 million flow e-commerce system
- Implement default page
- Redis主从复制过程
- [AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention
- 导入地址表分析(根据库文件名求出:导入函数数量、函数序号、函数名称)
猜你喜欢
For examination
New developments: new trends in cooperation between smartmesh and meshbox
One brush 314 sword finger offer 09 Implement queue (E) with two stacks
Temporal model: long-term and short-term memory network (LSTM)
大型互联网为什么禁止ip直连
Config learning notes component
Large factory technology implementation | industry solution series tutorials
导入地址表分析(根据库文件名求出:导入函数数量、函数序号、函数名称)
Codejock Suite Pro v20.3.0
The principle and common methods of multithreading and the difference between thread and runnable
随机推荐
Go语言条件,循环,函数
One brush 314 sword finger offer 09 Implement queue (E) with two stacks
vim指定行注释和解注释
Accumulation of applet knowledge points
JVM-第2章-类加载子系统(Class Loader Subsystem)
北京某信护网蓝队面试题目
基础贪心总结
时序模型:长短期记忆网络(LSTM)
Go并发和通道
Go language, array, pointer, structure
C language --- string + memory function
Do we media make money now? After reading this article, you will understand
Configuration of multi spanning tree MSTP
使用 Bitnami PostgreSQL Docker 镜像快速设置流复制集群
Codejock Suite Pro v20.3.0
【自娱自乐】构造笔记 week 2
Calculate the number of occurrences of a character
开源项目推荐:3D点云处理软件ParaView,基于Qt和VTK
How can poor areas without networks have money to build networks?
WPS brand was upgraded to focus on China. The other two domestic software were banned from going abroad with a low profile