如何使用标签将图像文件转换为CSV

时间:2019-05-17 12:07:39

标签: python pandas numpy

测试文件夹的文件夹名称为0到9。0-9文件夹包括相应的手写数字图像。我想将图像转换为单个test.csv文件,以使第一列给出数字的标签(即0-9),其余列给出图像的像素值。

我创建了csv,但标签的第一列显示为空。


from scipy.misc import imread
import numpy as np
import pandas as pd
import os
import imageio
import glob
root = './test' 

# go through each directory in the root folder given above
for directory, subdirectories, files in os.walk(root):
    # go through each file in that directory
    for file in files:  
        # read the image file and extract its pixels
        im = imread(os.path.join(directory,file))
        value = im.flatten()
        value = np.hstack((directory[8:],value))
        df = pd.DataFrame(value).T
        df = df.sample(frac=1) # shuffle the dataset
        with open('test.csv', 'a') as dataset: 
            df.to_csv(dataset, header=False, index=False)

1 个答案:

答案 0 :(得分:0)

from scipy.misc import imread
import numpy as np
import pandas as pd
import os
import imageio
import glob
import pathlib
v = []
for i,files in enumerate(pathlib.Path('./Train').glob('*/**/*.png')):
    im = imread(files.as_posix())
    value = im.flatten()
    value = np.hstack((int(files.parent.name),value))
    v.append(value)
df = pd.DataFrame(v)
df = df.sample(frac=1)
df.to_csv('train.csv',header=False,index=False)

这就是我纠正代码的方式。