我有一个名为的文件夹: ' LIDC-IDRI' 在这个文件夹里面我有一些名为的其他文件夹 ' LIDC-IDRI-0001' LIDC-IDRI-0002' LIDC-IDRI-0003',... 每个子文件夹都包含许多图像。
我想要做的是遍历所有子文件夹中的所有图像,并使用' imshow'功能,任何人都可以帮助我吗?
任何帮助都将不胜感激。
答案 0 :(得分:0)
@ honar.cs ,根据您的问题陈述,我试图解决您的问题。
在这里,我想显示
LIDC-IDRI-0001
,LIDC-IDRI-0002
,LIDC-IDRI-0003
,LIDC-IDRI-0004
目录中的所有png和jpg图像。
H:\RISHIKESHAGRAWANI\PROJECTS\SOF\DISPLAYIMAGES
└───LIDC-IDRI
│ show_images.md
│ show_images.py
│ show_images_temp.py
│
├───LIDC-IDRI-0001
│ download.jpg
│ Hacker.jpg
│
├───LIDC-IDRI-0002
│ images.jpg
│
├───LIDC-IDRI-0003
│ internet.jpg
│ Internet.png
│
└───LIDC-IDRI-0004
RishikeshAgrawani-Hygull-Python.jpg
wallpaper-strange-funny-weird-crazy-absurd-awesome-592.jpg
waterfalls.jpg
numpy - pip install numpy
matplotlib - pip install matplotlib
枕头 - pip install Pillow
show_images.py
import os
import json
import glob
import numpy as np
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
image_formats = ["png", "jpg"]; # Let suppose we want to display png & jpg images (specify more if you want)
def show_images(image_file_name):
print("Displaying ", image_file_name)
img=mpimg.imread(image_file_name)
imgplot = plt.imshow(img)
plt.show()
def get_image_paths(current_dir):
files = os.listdir(current_dir);
paths = []; # To store relative paths of all png and jpg images
for file in files:
file = file.strip()
if os.path.isdir(file) and 'LIDC-IDRI-' in file:
for image_format in image_formats:
image_paths = glob.glob(os.path.join(".", file, "*." + image_format))
if image_paths:
paths.extend(image_paths);
return paths
if __name__ == "__main__":
image_paths = get_image_paths(".");
print(json.dumps(image_paths, indent=4))
# Display all images inside image_paths
for image_path in image_paths:
show_images(image_path);
print('\n')
使用LIDC-IDRI
命令打开终端并在cd
目录中导航,然后运行以下命令。
python show_images.py
图像将逐个打开(一旦关闭第一张图像,将显示第二张图像,依此类推)。
[
".\\LIDC-IDRI-0001\\download.jpg",
".\\LIDC-IDRI-0001\\Hacker.jpg",
".\\LIDC-IDRI-0002\\images.jpg",
".\\LIDC-IDRI-0003\\Internet.png",
".\\LIDC-IDRI-0003\\internet.jpg",
".\\LIDC-IDRI-0004\\RishikeshAgrawani-Hygull-Python.jpg",
".\\LIDC-IDRI-0004\\wallpaper-strange-funny-weird-crazy-absurd-awesome-592.jpg",
".\\LIDC-IDRI-0004\\waterfalls.jpg"
]