使用imshow python在一个目录中显示多个图像

问题描述 投票:0回答:1

我试图在一个目录中显示多个图像。

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显示多个图像?

谢谢!

python matplotlib imshow
1个回答
0
投票

问题出在这里

fig, ax = plt.subplots(nrows=20, ncols=2, figsize=(15,6))

这使得ax成为形状np.array(20,2)。所以当你做for a in ax时,anp.array2轴。要解决此问题,请将以下行更改为:

for a in ax.flatten():
© www.soinside.com 2019 - 2024. All rights reserved.