Python点滴(八)—图片序列化转为csv格式中遇到的一些问题

   我在将数字图片转化为数组并保存在csv文件当中出现了一个问题,无法将图片数组存为我想要的形式,即一个excel格中放一个灰度值的样子,问题最终被解决现记录如下:

[python]  view plain  copy
  1. a=[[1,2,3,4,5],  
  2. [6,7,8,5,9,0],  
  3. ['a','b','e','q','h']]  
  4.   
  5. with open('test.csv','wb') as myfile:  
  6.        mywriter=csv.writer(myfile)  
  7.        mywriter.writerows(a)        #多行写入  'wb'写入防止隔行  
  8.       
  9.   
  10. type(a)  
  11. Out[159]: list  


空格、小数点并不影响数据的写入:

[python]  view plain  copy
  1. a=[[1.,    2.,    3.,    4.,    5.],  
  2. [6.,    7.,    8.,    5.,    9.,    0.],  
  3. ['a',    'b',    'e',    'q',    'h']]  
  4.   
  5. with open('test.csv','wb') as myfile:  
  6.        mywriter=csv.writer(myfile)  
  7.        mywriter.writerows(a)  


[python]  view plain  copy
  1. u=[[34,45,56],[45,234,567]]  
  2.   
  3. y=[45,23,22]  
  4.   
  5. u.append(y)  
  6.   
  7. u  
  8. Out[165]: [[344556], [45234567], [452322]]  
  9.   
  10. with open('test.csv','wb') as myfile:  
  11.        mywriter=csv.writer(myfile)  
  12.        mywriter.writerows(u)  
  13.       
  14.   
  15. type(u)  
  16. Out[167]: list     #注意通过append方式加入的list 在csv当中重占一行  



而如果我们将list转为mat形式的话:

[python]  view plain  copy
  1. u=numpy.mat(u)  
  2.   
  3. u  
  4. Out[169]:   
  5. matrix([[ 34,  45,  56],  
  6.         [ 45234567],  
  7.         [ 45,  23,  22]])  
  8.   
  9. with open('test.csv','wb') as myfile:  
  10.        mywriter=csv.writer(myfile)  
  11.        mywriter.writerows(u)            # u为matrix矩阵  
  12.       


[python]  view plain  copy
  1. from PIL import Image                         
  2. hwLabels=[]  
  3. img = Image.open('C:\\Anaconda\\trainingNum\\0-0.jpg')  
  4. img_ndarray=numpy.asarray(img,dtype='float64')/256      #转为array  
  5.   
  6.   
  7. type(img_ndarray)  
  8. Out[172]: numpy.ndarray  
  9.   
  10. img_ndarray[0]  
  11. Out[173]:   
  12. array([ 0.00390625,  0.        ,  0.0078125 ,  0.        ,  0.00390625,  
  13.         0.        ,  0.06640625,  0.13671875,  0.98828125,  0.66796875,  
  14.         0.125     ,  0.125     ,  0.11328125,  0.        ,  0.00390625,  
  15.         0.        ,  0.        ,  0.        ,  0.        ,  0.        ])  
  16.   
  17. type(img_ndarray[0])  
  18. Out[174]: numpy.ndarray  
  19.   
  20. img_ndarray  
  21. Out[175]:   
  22. array([[ 0.00390625,  0.        ,  0.0078125 ,  0.        ,  0.00390625,  
  23.          0.        ,  0.06640625,  0.13671875,  0.98828125,  0.66796875,  
  24.          0.125     ,  0.125     ,  0.11328125,  0.        ,  0.00390625,  
  25.          0.        ,  0.        ,  0.        ,  0.        ,  0.        ],  
  26.        [ 0.00390625,  0.        ,  0.        ,  0.01171875,  0.        ,  
  27.          0.14453125,  0.76953125,  0.98828125,  0.99609375,  0.9921875 ,  
  28.          0.99609375,  0.9921875 ,  0.9140625 ,  0.2421875 ,  0.        ,  
  29.          0.00390625,  0.        ,  0.        ,  0.        ,  0.        ],  
  30.        [ 0.        ,  0.01171875,  0.        ,  0.01171875,  0.0703125 ,  
  31.          0.7734375 ,  0.99609375,  0.99609375,  0.99609375,  0.98046875,  
  32.          0.99609375,  0.9921875 ,  0.99609375,  0.85546875,  0.23828125,  
  33.          0.        ,  0.        ,  0.        ,  0.        ,  0.        ],  
  34.        [ 0.        ,  0.        ,  0.        ,  0.        ,  0.8984375 ,  
  35.          0.9765625 ,  0.9921875 ,  0.234375  ,  0.12109375,  0.13671875,  
  36.          0.1328125 ,  0.890625  ,  0.98828125,  0.9921875 ,  0.3671875 ,  
  37.          0.        ,  0.        ,  0.        ,  0.        ,  0.        ],  
  38.        [ 0.        ,  0.        ,  0.        ,  0.00390625,  0.99609375,  
  39.          0.99609375,  0.90625   ,  0.11328125,  0.        ,  0.01171875,  
  40.          0.        ,  0.        ,  0.99609375,  0.99609375,  0.984375  ,  
  41.          0.1328125 ,  0.        ,  0.        ,  0.        ,  0.        ],  
  42.        [ 0.        ,  0.015625  ,  0.        ,  0.        ,  0.99609375,  
  43.          0.98828125,  0.61328125,  0.05078125,  0.        ,  0.00390625,  
  44.          0.        ,  0.0078125 ,  0.9921875 ,  0.98828125,  0.99609375,  
  45.          0.1328125 ,  0.        ,  0.        ,  0.        ,  0.        ],  
  46.        [ 0.0078125 ,  0.        ,  0.        ,  0.015625  ,  0.99609375,  
  47.          0.99609375,  0.60546875,  0.03515625,  0.00390625,  0.        ,  
  48.          0.015625  ,  0.        ,  0.99609375,  0.99609375,  0.99609375,  
  49.          0.11328125,  0.        ,  0.        ,  0.        ,  0.        ],  
  50.        [ 0.        ,  0.00390625,  0.00390625,  0.        ,  0.9921875 ,  
  51.          0.9921875 ,  0.91796875,  0.11328125,  0.        ,  0.0078125 ,  
  52.          0.        ,  0.        ,  0.9921875 ,  0.99609375,  0.98828125,  
  53.          0.13671875,  0.        ,  0.        ,  0.        ,  0.        ],  
  54.        [ 0.        ,  0.        ,  0.        ,  0.        ,  0.9921875 ,  
  55.          0.99609375,  0.99609375,  0.125     ,  0.0078125 ,  0.        ,  
  56.          0.        ,  0.        ,  0.234375  ,  0.99609375,  0.98828125,  
  57.          0.12890625,  0.        ,  0.        ,  0.        ,  0.        ],  
  58.        [ 0.        ,  0.        ,  0.        ,  0.        ,  0.9921875 ,  
  59.          0.99609375,  0.99609375,  0.125     ,  0.        ,  0.        ,  
  60.          0.        ,  0.        ,  0.9921875 ,  0.9921875 ,  0.99609375,  
  61.          0.1171875 ,  0.        ,  0.        ,  0.        ,  0.        ],  
  62.        [ 0.        ,  0.        ,  0.        ,  0.        ,  0.9921875 ,  
  63.          0.99609375,  0.99609375,  0.125     ,  0.0078125 ,  0.        ,  
  64.          0.0078125 ,  0.        ,  0.99609375,  0.99609375,  0.9921875 ,  
  65.          0.125     ,  0.        ,  0.        ,  0.        ,  0.        ],  
  66.        [ 0.        ,  0.        ,  0.        ,  0.        ,  0.9921875 ,  
  67.          0.99609375,  0.99609375,  0.125     ,  0.        ,  0.01171875,  
  68.          0.015625  ,  0.        ,  0.9921875 ,  0.984375  ,  0.99609375,  
  69.          0.11328125,  0.        ,  0.        ,  0.        ,  0.        ],  
  70.        [ 0.        ,  0.        ,  0.        ,  0.        ,  0.9921875 ,  
  71.          0.99609375,  0.99609375,  0.125     ,  0.        ,  0.        ,  
  72.          0.        ,  0.00390625,  0.99609375,  0.9765625 ,  0.99609375,  
  73.          0.1328125 ,  0.        ,  0.        ,  0.        ,  0.        ],  
  74.        [ 0.        ,  0.        ,  0.        ,  0.        ,  0.9921875 ,  
  75.          0.99609375,  0.99609375,  0.125     ,  0.        ,  0.01171875,  
  76.          0.        ,  0.        ,  0.671875  ,  0.99609375,  0.98828125,  
  77.          0.125     ,  0.        ,  0.        ,  0.        ,  0.        ],  
  78.        [ 0.        ,  0.        ,  0.        ,  0.        ,  0.9921875 ,  
  79.          0.99609375,  0.99609375,  0.125     ,  0.        ,  0.        ,  
  80.          0.0078125 ,  0.        ,  0.12109375,  0.99609375,  0.99609375,  
  81.          0.125     ,  0.        ,  0.        ,  0.        ,  0.        ],  
  82.        [ 0.        ,  0.        ,  0.        ,  0.        ,  0.9921875 ,  
  83.          0.99609375,  0.99609375,  0.125     ,  0.00390625,  0.00390625,  
  84.          0.        ,  0.        ,  0.88671875,  0.98828125,  0.9921875 ,  
  85.          0.12890625,  0.        ,  0.        ,  0.        ,  0.        ],  
  86.        [ 0.        ,  0.        ,  0.        ,  0.00390625,  0.890625  ,  
  87.          0.99609375,  0.99609375,  0.23046875,  0.11328125,  0.        ,  
  88.          0.0703125 ,  0.12890625,  0.9921875 ,  0.9921875 ,  0.921875  ,  
  89.          0.109375  ,  0.        ,  0.        ,  0.        ,  0.        ],  
  90.        [ 0.        ,  0.015625  ,  0.00390625,  0.        ,  0.07421875,  
  91.          0.76171875,  0.98828125,  0.99609375,  0.9140625 ,  0.375     ,  
  92.          0.76171875,  0.99609375,  0.98828125,  0.859375  ,  0.24609375,  
  93.          0.        ,  0.        ,  0.        ,  0.        ,  0.        ],  
  94.        [ 0.01171875,  0.        ,  0.        ,  0.00390625,  0.        ,  
  95.          0.140625  ,  0.7734375 ,  0.984375  ,  0.98828125,  0.99609375,  
  96.          0.99609375,  0.98046875,  0.9296875 ,  0.22265625,  0.        ,  
  97.          0.01171875,  0.        ,  0.        ,  0.        ,  0.        ],  
  98.        [ 0.        ,  0.        ,  0.01171875,  0.0078125 ,  0.        ,  
  99.          0.        ,  0.0859375 ,  0.125     ,  0.99609375,  0.99609375,  
  100.          0.99609375,  0.23046875,  0.1015625 ,  0.015625  ,  0.        ,  
  101.          0.        ,  0.        ,  0.        ,  0.        ,  0.        ]])  

此时数组是无法写入的:报错如下(数组array 矩阵matrix 都无法成功写入)

[python]  view plain  copy
  1. with open('test.csv','wb') as myfile:  
  2.       mywriter=csv.writer(myfile)  
  3.       mywriter.writerows(img_ndarray[0])  
  4.       
  5. ---------------------------------------------------------------------------  
  6. Error                                     Traceback (most recent call last)  
  7. <ipython-input-176-2aafd00022b3in <module>()  
  8.       1 with open('test.csv','wb') as myfile:  
  9.       2       mywriter=csv.writer(myfile)  
  10. ----> 3       mywriter.writerows(img_ndarray[0])  
  11.       4   
  12.   
  13. Error: sequence expected!!!!!!!!!!  
[python]  view plain  copy
  1. img_ndarray[0][0]  
  2. Out[178]: 0.00390625  
  3. #可以访问内部值  
  4.   
  5. type(img_ndarray[0])  
  6. Out[180]: numpy.ndarray  
  7.   
  8. type(img_ndarray[0][0])  
  9. Out[181]: numpy.float64  


转为list格式后可以输入写入:

b=[]

[python]  view plain  copy
  1. for i in range(20):  
  2.     b.append(img_ndarray[i].tolist())  
  3.       
  4.   
  5. type(b)  
  6. Out[183]: list  
  7.   
  8. numpy.shape(b)  
  9. Out[184]: (20L20L)  
  10.   
  11. b[0]  
  12. Out[185]:   
  13. [0.00390625,  
  14.  0.0,  
  15.  0.0078125,  
  16.  0.0,  
  17.  0.00390625,  
  18.  0.0,  
  19.  0.06640625,  
  20.  0.13671875,  
  21.  0.98828125,  
  22.  0.66796875,  
  23.  0.125,  
  24.  0.125,  
  25.  0.11328125,  
  26.  0.0,  
  27.  0.00390625,  
  28.  0.0,  
  29.  0.0,  
  30.  0.0,  
  31.  0.0,  
  32.  0.0]  
  33.   
  34. b[0][0]  
  35. Out[186]: 0.00390625  
  36.   
  37. b  
  38. Out[187]:        #这是一张图片  
  39. [[0.00390625,  
  40.   0.0,  
  41.   0.0078125,  
  42.   0.0,  
  43.   0.00390625,  
  44.   0.0,  
  45.   0.06640625,  
  46.   0.13671875,  
  47.   0.98828125,  
  48.   0.66796875,  
  49.   0.125,  
  50.   0.125,  
  51.   0.11328125,  
  52.   0.0,  
  53.   0.00390625,  
  54.   0.0,  
  55.   0.0,  
  56.   0.0,  
  57.   0.0,  
  58.   0.0],  
  59.  [0.00390625,  
  60.   0.0,  
  61.   0.0,  
  62.   0.01171875,  
  63.   0.0,  
  64.   0.14453125,  
  65.   0.76953125,  
  66.   0.98828125,  
  67.   0.99609375,  
  68.   0.9921875,  
  69.   0.99609375,  
  70.   0.9921875,  
  71.   0.9140625,  
  72.   0.2421875,  
  73.   0.0,  
  74.   0.00390625,  
  75.   0.0,  
  76.   0.0,  
  77.   0.0,  
  78.   0.0],  
  79.  [0.0,  
  80.   0.01171875,  
  81.   0.0,  
  82.   0.01171875,  
  83.   0.0703125,  
  84.   0.7734375,  
  85.   0.99609375,  
  86.   0.99609375,  
  87.   0.99609375,  
  88.   0.98046875,  
[python]  view plain  copy
  1. with open('test.csv','wb') as myfile:  
  2.       mywriter=csv.writer(myfile)  
  3.       mywriter.writerows(b)<span style="font-size:14px;">  
  4. </span>  

数据写入成功!


将两张图片读入到csv文件当中,并在末尾添加标签'100'

[python]  view plain  copy
  1. from PIL import Image                       
  2. imgarray=[]  
  3. img = Image.open('C:\\Anaconda\\trainingNum\\0-0.jpg')  
  4. img_ndarray=numpy.asarray(img,dtype='float64')/256    #归一化  
  5. img_ndarray=numpy.ndarray.flatten(img_ndarray)      #压平为一维向量  一维数组  
  6. img_ndarray1=img_ndarray.tolist()                 #将array格式转为list格式  
  7. img_ndarray1.extend([100])                    #末尾添加  
  8. imgarray.append(img_ndarray1)                #只有转化为list格式才能使用append方法  
  9.   
  10.   
  11. img = Image.open('C:\\Anaconda\\trainingNum\\1-6.jpg')  
  12. img_ndarray2=numpy.asarray(img,dtype='float64')/256  
  13. img_ndarray2=(numpy.ndarray.flatten(img_ndarray2))  
  14. img_ndarray2=img_ndarray2.tolist()  
  15. img_ndarray2.extend([100])  
  16. imgarray.append(img_ndarray2)  
  17.   
  18.   
  19. with open('test.csv','wb') as myfile:  
  20.       mywriter=csv.writer(myfile)  
  21.       mywriter.writerows(imgarray)          #list格式才可以使用mywriter写入  array和matrix格式都不可以  
  22.       



终极完美boss!

[python]  view plain  copy
  1. from PIL import Image  
  2. import numpy  
  3. import os  
  4. trainingFileList = os.listdir('C:\\Anaconda\\trainingNum2')             
  5. m = len(trainingFileList)                   
  6. trainingMat=[]  
  7. hwLabels=[]  
  8. def img2vector(filename):  
  9.   
  10.     img = Image.open(filename)  
  11.     img_ndarray=numpy.asarray(img,dtype='int')  
  12.     returnVect=numpy.ndarray.flatten(img_ndarray)  
  13.     returnVect=returnVect.tolist()  
  14.     return returnVect  
  15. for i in range(m):  
  16.        fileNameStr = trainingFileList[i]                    
  17.        fileStr = fileNameStr.split('.')[0]                  
  18.        classNumStr = int(fileStr.split('-')[0])             
  19.        hwLabels.append(classNumStr)         
  20.        Vect=img2vector('C:\\Anaconda\\trainingNum2\\%s' % fileNameStr)    
  21.        Vect.extend([classNumStr])          
  22.        trainingMat.append(Vect)  
  23.   
  24. with open('test.csv','wb') as myfile:  
  25.        mywriter=csv.writer(myfile)  
  26.        mywriter.writerows(trainingMat)  
  27.       


版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_14959801/article/details/58603728
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值