我想创建一个矩阵子图并显示每个BMP文件,从一个目录,在不同的子图中,但我找不到适合我的问题的解决方案,有人可以帮助我吗?
这是我的代码:
import os, sys
from PIL import Image
import matplotlib.pyplot as plt
from glob import glob
bmps = glob('*trace*.bmp')
fig, axes = plt.subplots(3, 3)
for arch in bmps:
i = Image.open(arch)
iar = np.array(i)
for i in range(3):
for j in range(3):
axes[i, j].plot(iar)
plt.subplots_adjust(wspace=0, hspace=0)
plt.show()
Run Code Online (Sandbox Code Playgroud)
执行后我遇到以下错误:
ral*_*htp 19
本机matplotlib仅支持PNG图像,请参阅http://matplotlib.org/users/image_tutorial.html
然后总是读取图像的方式 - 绘制图像
读图像
img1 = mpimg.imread('stinkbug1.png')
img2 = mpimg.imread('stinkbug2.png')
Run Code Online (Sandbox Code Playgroud)
情节图像(2个子图)
plt.figure(1)
plt.subplot(211)
plt.imshow(img1)
plt.subplot(212)
plt.imshow(img2)
plt.show()
Run Code Online (Sandbox Code Playgroud)
按照http://matplotlib.org/users/image_tutorial.html上的教程(因为导入库)
这是一个用matplotlib绘制bmps的线程:为什么bmp图像在IPython-notebook上用mattlotlib的plt.imshow显示为错误的颜色?
bmp 具有三个颜色通道,加上高度和宽度,使其形状为 (h,w,3)。我相信绘制图像会给您带来错误,因为该图仅接受二维。您可以对图像进行灰度化,这将产生仅二维(h,w)的矩阵。
在不知道图像尺寸的情况下,您可以执行以下操作:
for idx, arch in enumerate(bmps):
i = idx % 3 # Get subplot row
j = idx // 3 # Get subplot column
image = Image.open(arch)
iar_shp = np.array(image).shape # Get h,w dimensions
image = image.convert('L') # convert to grayscale
# Load grayscale matrix, reshape to dimensions of color bmp
iar = np.array(image.getdata()).reshape(iar_shp[0], iar_shp[1])
axes[i, j].plot(iar)
plt.subplots_adjust(wspace=0, hspace=0)
plt.show()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36901 次 |
| 最近记录: |