我在将数字图片转化为数组并保存在csv文件当中出现了一个问题,无法将图片数组存为我想要的形式,即一个excel格中放一个灰度值的样子,问题最终被解决现记录如下:
a=[[1,2,3,4,5],
[6,7,8,5,9,0],
['a','b','e','q','h']]
with open('test.csv','wb') as myfile:
mywriter=csv.writer(myfile)
mywriter.writerows(a) #多行写入 'wb'写入防止隔行
type(a)
Out[159]: list
空格、小数点并不影响数据的写入:
a=[[1., 2., 3., 4., 5.],
[6., 7., 8., 5., 9., 0.],
['a', 'b', 'e', 'q', 'h']]
with open('test.csv','wb') as myfile:
mywriter=csv.writer(myfile)
mywriter.writerows(a)
u=[[34,45,56],[45,234,567]]
y=[45,23,22]
u.append(y)
u
Out[165]: [[34, 45, 56], [45, 234, 567], [45, 23, 22]]
with open('test.csv','wb') as myfile:
mywriter=csv.writer(myfile)
mywriter.writerows(u)
type(u)
Out[167]: list #注意通过append方式加入的list 在csv当中重占一行
而如果我们将list转为mat形式的话:
u=numpy.mat(u)
u
Out[169]:
matrix([[ 34, 45, 56],
[ 45, 234, 567],
[ 45, 23, 22]])
with open('test.csv','wb') as myfile:
mywriter=csv.writer(myfile)
mywriter.writerows(u) # u为matrix矩阵
from PIL import Image
hwLabels=[]
img = Image.open('C:\\Anaconda\\trainingNum\\0-0.jpg')
img_ndarray=numpy.asarray(img,dtype='float64')/256 #转为array
type(img_ndarray)
Out[172]: numpy.ndarray
img_ndarray[0]
Out[173]:
array([ 0.00390625, 0. , 0.0078125 , 0. , 0.00390625,
0. , 0.06640625, 0.13671875, 0.98828125, 0.66796875,
0.125 , 0.125 , 0.11328125, 0. , 0.00390625,
0. , 0. , 0. , 0. , 0. ])
type(img_ndarray[0])
Out[174]: numpy.ndarray
img_ndarray
Out[175]:
array([[ 0.00390625, 0. , 0.0078125 , 0. , 0.00390625,
0. , 0.06640625, 0.13671875, 0.98828125, 0.66796875,
0.125 , 0.125 , 0.11328125, 0. , 0.00390625,
0. , 0. , 0. , 0. , 0. ],
[ 0.00390625, 0. , 0. , 0.01171875, 0. ,
0.14453125, 0.76953125, 0.98828125, 0.99609375, 0.9921875 ,
0.99609375, 0.9921875 , 0.9140625 , 0.2421875 , 0. ,
0.00390625, 0. , 0. , 0. , 0. ],
[ 0. , 0.01171875, 0. , 0.01171875, 0.0703125 ,
0.7734375 , 0.99609375, 0.99609375, 0.99609375, 0.98046875,
0.99609375, 0.9921875 , 0.99609375, 0.85546875, 0.23828125,
0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0.8984375 ,
0.9765625 , 0.9921875 , 0.234375 , 0.12109375, 0.13671875,
0.1328125 , 0.890625 , 0.98828125, 0.9921875 , 0.3671875 ,
0. , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0.00390625, 0.99609375,
0.99609375, 0.90625 , 0.11328125, 0. , 0.01171875,
0. , 0. , 0.99609375, 0.99609375, 0.984375 ,
0.1328125 , 0. , 0. , 0. , 0. ],
[ 0. , 0.015625 , 0. , 0. , 0.99609375,
0.98828125, 0.61328125, 0.05078125, 0. , 0.00390625,
0. , 0.0078125 , 0.9921875 , 0.98828125, 0.99609375,
0.1328125 , 0. , 0. , 0. , 0. ],
[ 0.0078125 , 0. , 0. , 0.015625 , 0.99609375,
0.99609375, 0.60546875, 0.03515625, 0.00390625, 0. ,
0.015625 , 0. , 0.99609375, 0.99609375, 0.99609375,
0.11328125, 0. , 0. , 0. , 0. ],
[ 0. , 0.00390625, 0.00390625, 0. , 0.9921875 ,
0.9921875 , 0.91796875, 0.11328125, 0. , 0.0078125 ,
0. , 0. , 0.9921875 , 0.99609375, 0.98828125,
0.13671875, 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0.9921875 ,
0.99609375, 0.99609375, 0.125 , 0.0078125 , 0. ,
0. , 0. , 0.234375 , 0.99609375, 0.98828125,
0.12890625, 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0.9921875 ,
0.99609375, 0.99609375, 0.125 , 0. , 0. ,
0. , 0. , 0.9921875 , 0.9921875 , 0.99609375,
0.1171875 , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0.9921875 ,
0.99609375, 0.99609375, 0.125 , 0.0078125 , 0. ,
0.0078125 , 0. , 0.99609375, 0.99609375, 0.9921875 ,
0.125 , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0.9921875 ,
0.99609375, 0.99609375, 0.125 , 0. , 0.01171875,
0.015625 , 0. , 0.9921875 , 0.984375 , 0.99609375,
0.11328125, 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0.9921875 ,
0.99609375, 0.99609375, 0.125 , 0. , 0. ,
0. , 0.00390625, 0.99609375, 0.9765625 , 0.99609375,
0.1328125 , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0.9921875 ,
0.99609375, 0.99609375, 0.125 , 0. , 0.01171875,
0. , 0. , 0.671875 , 0.99609375, 0.98828125,
0.125 , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0.9921875 ,
0.99609375, 0.99609375, 0.125 , 0. , 0. ,
0.0078125 , 0. , 0.12109375, 0.99609375, 0.99609375,
0.125 , 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0. , 0.9921875 ,
0.99609375, 0.99609375, 0.125 , 0.00390625, 0.00390625,
0. , 0. , 0.88671875, 0.98828125, 0.9921875 ,
0.12890625, 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0. , 0.00390625, 0.890625 ,
0.99609375, 0.99609375, 0.23046875, 0.11328125, 0. ,
0.0703125 , 0.12890625, 0.9921875 , 0.9921875 , 0.921875 ,
0.109375 , 0. , 0. , 0. , 0. ],
[ 0. , 0.015625 , 0.00390625, 0. , 0.07421875,
0.76171875, 0.98828125, 0.99609375, 0.9140625 , 0.375 ,
0.76171875, 0.99609375, 0.98828125, 0.859375 , 0.24609375,
0. , 0. , 0. , 0. , 0. ],
[ 0.01171875, 0. , 0. , 0.00390625, 0. ,
0.140625 , 0.7734375 , 0.984375 , 0.98828125, 0.99609375,
0.99609375, 0.98046875, 0.9296875 , 0.22265625, 0. ,
0.01171875, 0. , 0. , 0. , 0. ],
[ 0. , 0. , 0.01171875, 0.0078125 , 0. ,
0. , 0.0859375 , 0.125 , 0.99609375, 0.99609375,
0.99609375, 0.23046875, 0.1015625 , 0.015625 , 0. ,
0. , 0. , 0. , 0. , 0. ]])
此时数组是无法写入的:报错如下(数组array 矩阵matrix 都无法成功写入)
with open('test.csv','wb') as myfile:
mywriter=csv.writer(myfile)
mywriter.writerows(img_ndarray[0])
---------------------------------------------------------------------------
Error Traceback (most recent call last)
<ipython-input-176-2aafd00022b3> in <module>()
1 with open('test.csv','wb') as myfile:
2 mywriter=csv.writer(myfile)
----> 3 mywriter.writerows(img_ndarray[0])
4
Error: sequence expected!!!!!!!!!!
img_ndarray[0][0]
Out[178]: 0.00390625
#可以访问内部值
type(img_ndarray[0])
Out[180]: numpy.ndarray
type(img_ndarray[0][0])
Out[181]: numpy.float64
转为list格式后可以输入写入:
b=[]
for i in range(20):
b.append(img_ndarray[i].tolist())
type(b)
Out[183]: list
numpy.shape(b)
Out[184]: (20L, 20L)
b[0]
Out[185]:
[0.00390625,
0.0,
0.0078125,
0.0,
0.00390625,
0.0,
0.06640625,
0.13671875,
0.98828125,
0.66796875,
0.125,
0.125,
0.11328125,
0.0,
0.00390625,
0.0,
0.0,
0.0,
0.0,
0.0]
b[0][0]
Out[186]: 0.00390625
b
Out[187]: #这是一张图片
[[0.00390625,
0.0,
0.0078125,
0.0,
0.00390625,
0.0,
0.06640625,
0.13671875,
0.98828125,
0.66796875,
0.125,
0.125,
0.11328125,
0.0,
0.00390625,
0.0,
0.0,
0.0,
0.0,
0.0],
[0.00390625,
0.0,
0.0,
0.01171875,
0.0,
0.14453125,
0.76953125,
0.98828125,
0.99609375,
0.9921875,
0.99609375,
0.9921875,
0.9140625,
0.2421875,
0.0,
0.00390625,
0.0,
0.0,
0.0,
0.0],
[0.0,
0.01171875,
0.0,
0.01171875,
0.0703125,
0.7734375,
0.99609375,
0.99609375,
0.99609375,
0.98046875,
with open('test.csv','wb') as myfile:
mywriter=csv.writer(myfile)
mywriter.writerows(b)
数据写入成功!
将两张图片读入到csv文件当中,并在末尾添加标签'100'
from PIL import Image
imgarray=[]
img = Image.open('C:\\Anaconda\\trainingNum\\0-0.jpg')
img_ndarray=numpy.asarray(img,dtype='float64')/256 #归一化
img_ndarray=numpy.ndarray.flatten(img_ndarray) #压平为一维向量 一维数组
img_ndarray1=img_ndarray.tolist() #将array格式转为list格式
img_ndarray1.extend([100]) #末尾添加
imgarray.append(img_ndarray1) #只有转化为list格式才能使用append方法
img = Image.open('C:\\Anaconda\\trainingNum\\1-6.jpg')
img_ndarray2=numpy.asarray(img,dtype='float64')/256
img_ndarray2=(numpy.ndarray.flatten(img_ndarray2))
img_ndarray2=img_ndarray2.tolist()
img_ndarray2.extend([100])
imgarray.append(img_ndarray2)
with open('test.csv','wb') as myfile:
mywriter=csv.writer(myfile)
mywriter.writerows(imgarray) #list格式才可以使用mywriter写入 array和matrix格式都不可以
终极完美boss!
from PIL import Image
import numpy
import os
trainingFileList = os.listdir('C:\\Anaconda\\trainingNum2')
m = len(trainingFileList)
trainingMat=[]
hwLabels=[]
def img2vector(filename):
img = Image.open(filename)
img_ndarray=numpy.asarray(img,dtype='int')
returnVect=numpy.ndarray.flatten(img_ndarray)
returnVect=returnVect.tolist()
return returnVect
for i in range(m):
fileNameStr = trainingFileList[i]
fileStr = fileNameStr.split('.')[0]
classNumStr = int(fileStr.split('-')[0])
hwLabels.append(classNumStr)
Vect=img2vector('C:\\Anaconda\\trainingNum2\\%s' % fileNameStr)
Vect.extend([classNumStr])
trainingMat.append(Vect)
with open('test.csv','wb') as myfile:
mywriter=csv.writer(myfile)
mywriter.writerows(trainingMat)