当前位置:网站首页>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
边栏推荐
- 王启亨谈Web3.0与价值互联网“通证交换”
- API IX JWT auth plug-in has an error. Risk announcement of information disclosure in response (cve-2022-29266)
- Redis master-slave replication process
- The length of the last word of the string
- JS regular détermine si le nom de domaine ou le chemin de port IP est correct
- [split of recursive number] n points K, split of limited range
- Config learning notes component
- C语言自编字符串处理函数——字符串分割、字符串填充等
- Metalife established a strategic partnership with ESTV and appointed its CEO Eric Yoon as a consultant
- Extract non duplicate integers
猜你喜欢
Cap theorem
Temporal model: long-term and short-term memory network (LSTM)
Codejock Suite Pro v20. three
Vision of building interstellar computing network
基于 TiDB 的 Apache APISIX 高可用配置中心的最佳实践
Spark 算子之coalesce与repartition
R语言中实现作图对象排列的函数总结
Mobile finance (for personal use)
Pgpool II 4.3 Chinese Manual - introductory tutorial
Spark 算子之filter使用
随机推荐
[AI weekly] NVIDIA designs chips with AI; The imperfect transformer needs to overcome the theoretical defect of self attention
For examination
MetaLife与ESTV建立战略合作伙伴关系并任命其首席执行官Eric Yoon为顾问
Go language, condition, loop, function
Application case of GPS Beidou high precision satellite time synchronization system
字符串排序
糖尿病眼底病变综述概要记录
Go language slice, range, set
Upgrade MySQL 5.1 to 5.68
How can poor areas without networks have money to build networks?
Basic concepts of website construction and management
【AI周报】英伟达用AI设计芯片;不完美的Transformer要克服自注意力的理论缺陷
多级缓存使用
计算某字符出现次数
PHP operators
Use bitnami PostgreSQL docker image to quickly set up stream replication clusters
ICE -- 源码分析
Load Balancer
Treatment of idempotency
JVM-第2章-类加载子系统(Class Loader Subsystem)