python - 使用 imshow python 在一个目录中显示多个图像
问题描述
我正在尝试在一个目录中显示多个图像。
import os
import cv2
import numpy as np
import random
import math
import matplotlib
from matplotlib.pyplot import imshow
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
%matplotlib inline
dir_path = 'img'
images = os.listdir(dir_path)
img_paths = [os.path.join(dir_path, i) for i in images]
img_paths.sort()
img_all = np.array([cv2.cvtColor(cv2.imread(p), cv2.COLOR_BGR2RGB) for p in img_paths])
def display_helper(images, cmap=None):
fig, ax = plt.subplots(nrows=20, ncols=2, figsize=(15,6))
for a in ax:
a.imshow(img, interpolation='none')
display_helper(img_all)
但我得到这个错误
AttributeError: 'numpy.ndarray' object has no attribute 'imshow'
如何使用 imshow 显示多个图像?
谢谢!
解决方案
问题就在这里
fig, ax = plt.subplots(nrows=20, ncols=2, figsize=(15,6))
这给出ax
了一个np.array
形状(20,2)
。所以当你这样做时for a in ax
,a
是一个np.array
轴2
。要修复它,请将以下行更改为:
for a in ax.flatten():
推荐阅读
- bash - 剪切命令不重定向
- apache-spark - 火花流:执行每日聚合
- c++ - 通过 DirectX12 工具包进行挑选
- android - Parse-SDK-Android:parse:1.24.0 有修复吗
- python - 相对 import_module 不知道包含包的名称
- outlook-addin - 调用方法需要提升权限:'mailbox.getUserIdentityToken'
- jekyll - kernel_require.rb 无法加载此类文件 jekyll
- dotnetnuke - 将 DNN (DotNetNuke) 迁移到 Godaddy Plesk
- acumatica - 如何使用 put 在 acumatica 中上传照片
- google-dfp - 亚马逊与 prebid 并行使用更简单的 GPT 和刷新