当前位置:网站首页>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
边栏推荐
- 删除字符串中出现次数最少的字符
- pywintypes. com_ Error: (- 2147221020, 'invalid syntax', none, none)
- 运维流程有多重要,听说一年能省下200万?
- Neodynamic Barcode Professional for WPF V11. 0
- 基于 TiDB 的 Apache APISIX 高可用配置中心的最佳实践
- CVPR 2022 quality paper sharing
- Implement default page
- Go并发和通道
- 【开源工具分享】单片机调试助手(示波/改值/日志) - LinkScope
- 多线程原理和常用方法以及Thread和Runnable的区别
猜你喜欢
Basic concepts of website construction and management
For examination
pgpool-II 4.3 中文手册 - 入门教程
Large factory technology implementation | industry solution series tutorials
腾讯Offer已拿,这99道算法高频面试题别漏了,80%都败在算法上
MySQL集群模式与应用场景
基于 TiDB 的 Apache APISIX 高可用配置中心的最佳实践
Partitionby of spark operator
MySQL optimistic lock to solve concurrency conflict
Cookie&Session
随机推荐
Multi level cache usage
Fastjon2 here he is, the performance is significantly improved, and he can fight for another ten years
Treatment of idempotency
js正則判斷域名或者IP的端口路徑是否正確
Partitionby of spark operator
Vision of building interstellar computing network
Go语言数组,指针,结构体
C language --- advanced pointer
多生成树MSTP的配置
时序模型:长短期记忆网络(LSTM)
Redis master-slave replication process
提取不重复的整数
mysql乐观锁解决并发冲突
dlopen/dlsym/dlclose的简单用法
时序模型:门控循环单元网络(GRU)
PS为图片添加纹理
Date date calculation in shell script
【自娱自乐】构造笔记 week 2
糖尿病眼底病变综述概要记录
Metalife established a strategic partnership with ESTV and appointed its CEO Eric Yoon as a consultant