手机版 欢迎访问it开发者社区(www.mfbz.cn)网站

当前位置: > 开发

遍历文件夹,写Execl的python脚本

时间:2021/5/17 19:36:11|来源:|点击: 次

遍历文件夹,写Execl的python脚本


分享一个遍历文件夹,写csv的脚本

import cv2
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

img_path = "./img_path"


def gci(filepath):
    file_list = []
    #遍历filepath下所有文件,包括子目录
    files = os.listdir(filepath)
    for fi in files:
        #print(fi)
        fi_d = os.path.join(filepath,fi)
        if os.path.isdir(fi_d):
            file_list = file_list + gci(fi_d)
        else:
            file = fi_d
            file_list.append(file)
    return file_list

file_name_list = gci(img_path)
print(len(file_name_list))

img_h = []
img_w = []
img_h_w = []
for name in file_name_list:
    #print(name)
    img = cv2.imread(name)
    try:
        img.shape
    except:
        print(name)
        continue
    img_shape = img.shape
    img_h.append(img_shape[0])
    img_w.append(img_shape[1])
    img_h_w.append(img_shape[0]*img_shape[1])

#total = []
#total.append(img_h)
#total.append(img_w)
#total.append(img_h_w)
#title = ['img_h', 'img_w', 'img_h_w']

img_h.sort()
img_w.sort()
img_h_w.sort()
total = {'img_h':img_h, 'img_w':img_w, 'img_h_w':img_h_w}
csv_obj = pd.DataFrame(total)
#print(csv_obj)
csv_obj.to_csv('./shape_csv.csv', encoding='gbk')

#file_w = open('img_h.txt', 'w')
#file_w.write(str(img_h))
#file_w.close()

#histgram
plt.figure()
n, bins, patches = plt.hist(x = img_h, bins = 'auto', color='r')
plt.grid(axis='y', alpha=0.75)
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.title('Img H Histogram')
maxfreq = n.max()
# 设置y轴的上限
plt.ylim(ymax=np.ceil(maxfreq / 10) * 10 if maxfreq % 10 else maxfreq + 10)

plt.figure()
n, bins, patches = plt.hist(x = img_w, bins = 'auto', color='g')
plt.title('Img W Histogram')

plt.figure()
n, bins, patches = plt.hist(x = np.sqrt(img_h_w), bins = 'auto', color='b')
plt.title('Img sqrt(h*w) Histogram')

plt.show()


Copyright © 2002-2019 某某自媒体运营 版权所有