python 批量读取文件夹的动漫美女图并显示

如果你觉得对你有用,请留下一个赞再走,谢谢!!

原始文件
在这里插入图片描述

代码

import os
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
"""批量读取文件夹的图片并显示"""
def ReFileName(dirPath):
    """
    :param dirPath: 文件夹路径
    :return:
    """
    # 对目录下的文件进行遍历
    for file in os.listdir(dirPath):
        # 判断是否是文件
        if os.path.isfile(os.path.join(dirPath, file)) == True:
           c= os.path.basename(file)
           name=dirPath+'\\'+c
           pil_im = mpimg.imread(name)

           plt.imshow(np.real(pil_im))
           plt.show()




if __name__ == '__main__':

    dirPath = r"C:\Users\Shineion\Desktop\动漫美女"
    ReFileName(dirPath)



结果

在这里插入图片描述

说明

代码 解释
os.path.join(dirPath, file) 把目录和文件名合成一个路径
os.path.basename(file) 返回文件名
name=dirPath+’\’+c 生成文件绝对路径

使用 matplotlib.image 读取图片
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

#name=图片绝对路径
pil_im = mpimg.imread(name)
plt.imshow(np.real(pil_im))
plt.show()

猜你喜欢

转载自blog.csdn.net/kobeyu652453/article/details/106943683