【发布时间】:2021-05-22 04:22:46
【问题描述】:
我在一个文件夹中有很多数字图像,我已经在 CSV 文件中转换了这些图像,但是就像在 MNIST 数据集中,当我们在 CSV 文件中转换一个数字时,它附加了一个标签列但是当我转换我的image 文件夹转换为 CSV ,我的 CSV 文件不包含标签列。
请帮助我如何在我自己创建的 CSV 文件中添加标签列。 我写的代码如下:-
from PIL import Image
import numpy as np
import sys
import os
import csv
# default format can be changed as needed
def createFileList(myDir, format='.jpg'):
fileList = []
print(myDir)
for root, dirs, files in os.walk(myDir, topdown=False):
for name in files:
if name.endswith(format):
fullName = os.path.join(root, name)
fileList.append(fullName)
return fileList
# load the original image
myFileList = createFileList('/path_to_directory_with_images/')
for file in myFileList:
print(file)
img_file = Image.open(file)
# img_file.show()
# get original image parameters...
width, height = img_file.size
format = img_file.format
mode = img_file.mode
# Make image Greyscale
img_grey = img_file.convert('L')
#img_grey.save('result.png')
#img_grey.show()
# Save Greyscale values
value = np.asarray(img_grey.getdata(), dtype=np.int).reshape((img_grey.size[1], img_grey.size[0]))
value = value.flatten()
print(value)
with open("name_you_want.csv", 'a') as f:
writer = csv.writer(f)
writer.writerow(value)
【问题讨论】:
标签: python machine-learning deep-learning mnist