鱼C论坛

 找回密码
 立即注册
查看: 3015|回复: 10

[已解决]如何读取文件夹里面的全部图片

[复制链接]
发表于 2019-4-2 14:23:27 | 显示全部楼层 |阅读模式
10鱼币
请问,如何用python读取文件夹里面的全部图片。然后储存到数组里面
最佳答案
2019-4-2 14:23:28
本帖最后由 My_A 于 2019-4-2 20:52 编辑
  1. import os

  2. path = "X:\\XXXXXXXXX"
  3. file_list = []

  4. def search(path):
  5.     lst = os.listdir()
  6.     for f in lst:
  7.         f = os.path.join(path,f)
  8.         if  os.path.isdir(f):
  9.             search(f)
  10.         elif  os.path.isfile(f)
  11.             suffix = ['.jpg','.png','.bmp']
  12.             for s in suffix:
  13.                 if f.endwith(s):
  14.                     file_list.append(f)
复制代码

最佳答案

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-4-2 14:23:28 From FishC Mobile | 显示全部楼层    本楼为最佳答案   
本帖最后由 My_A 于 2019-4-2 20:52 编辑
  1. import os

  2. path = "X:\\XXXXXXXXX"
  3. file_list = []

  4. def search(path):
  5.     lst = os.listdir()
  6.     for f in lst:
  7.         f = os.path.join(path,f)
  8.         if  os.path.isdir(f):
  9.             search(f)
  10.         elif  os.path.isfile(f)
  11.             suffix = ['.jpg','.png','.bmp']
  12.             for s in suffix:
  13.                 if f.endwith(s):
  14.                     file_list.append(f)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-4-2 14:26:36 | 显示全部楼层
二进制读取到内存???
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-4-2 14:31:22 | 显示全部楼层
没记错的话,可以用os的isdir()方法,
定位到目标目录后用for循环遍历就可以生成列表了。

不过最好还是获取目录下的文件名就行了。
操作的时候再读取
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-4-2 14:53:31 | 显示全部楼层
wongyusing 发表于 2019-4-2 14:31
没记错的话,可以用os的isdir()方法,
定位到目标目录后用for循环遍历就可以生成列表了。

我想全部读取出来,存储到矩阵里面。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-4-2 14:55:19 | 显示全部楼层
构造文件队列,阅读器,读取
你可以去百度下tensorflow的操作,b站也有视频
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-4-2 15:07:35 From FishC Mobile | 显示全部楼层
最后再  search() 就行了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-4-2 16:20:42 | 显示全部楼层
My_A 发表于 2019-4-2 15:07
最后再  search() 就行了

最后得到的是一个空列表
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-4-2 16:48:16 | 显示全部楼层
import os
import cv2
from itertools import chain

array_of_img = [] # this if for store all of the image data
# this function is for read image,the input is directory name
def read_directory(directory_name):
    # this loop is for read each image in this foder,directory_name is the foder name with images.
    for filename in os.listdir(r"./"+directory_name):
        #print(filename) #just for test
        #img is used to store the image data
        img = cv2.imread(directory_name + "/" + filename)
        img=list(chain(*img))
        array_of_img.append(img)
     
        #print(img)
        #print(array_of_img)
        
        
read_directory("Images")
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-4-2 20:44:42 | 显示全部楼层
飘香一剑 发表于 2019-4-2 16:20
最后得到的是一个空列表

把path 的内容换为你要的文件夹的路径
把suffix 列表的内容换为你搜索的图片的后缀名

最后在 search(path)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-4-2 20:59:36 | 显示全部楼层
如果要把图片中的二进制内容保存在列表中则可以加上:
from PIL import Image

pic_data = {}

for f in file_list:
        pic_data[f] = Image.open(f)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-9-11 10:13

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表