【发布时间】:2018-08-10 17:37:22
【问题描述】:
我已将图像转换为 csv 文件,它就像一个矩阵,但我希望它是单行。 如何将数据集中的所有图像转换为 csv 文件(每张图像为一行)。
这是我使用的代码:
from PIL import Image
import numpy as np
import os, os.path, time
format='.jpg'
myDir = "Lotus1"
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
fileList = createFileList(myDir)
fileFormat='.jpg'
for fileFormat in fileList:
format = '.jpg'
# get original image parameters...
width, height = fileList.size
format = fileList.format
mode = fileList.mode
# Make image Greyscale
img_grey = fileList.convert('L')
# Save Greyscale values
value = np.asarray(fileList.getdata(),dtype=np.float64).reshape((fileList.size[1],fileList.size[0]))
np.savetxt("img_pixels.csv", value, delimiter=',')
【问题讨论】:
-
写入一个新文件,将矩阵的内容写入一行?
-
您可以发布示例数据吗?
-
嗨@MohamedThasinah,我已经发布了输出和输入。
-
嗨@IMCoins,我没明白你的意思! :\