2017-02-22 52 views
0

我的問題有點類似於this one,但我無法弄清楚如何使它適用於我。我想任意大小的灰度BMP 0到255
之間轉換成列表的列表與值。例如:
如果輸入的是這樣的:grayscale bmp
輸出應該是:Python:將灰度BMP轉換爲列表清單

pic = [[255, 255, 255, 255, 255, 255, 255, 255, 255, 255], 
     [255, 255, 255, 255, 255, 255, 255, 255, 255, 255], 
     [255, 255, 127, 127, 127, 127, 127, 127, 255, 255], 
     [255, 255, 127, 127, 127, 127, 127, 127, 255, 255], 
     [255, 255, 127, 127, 0 , 0 , 127, 127, 255, 255], 
     [255, 255, 127, 127, 0 , 0 , 127, 127, 255, 255], 
     [255, 255, 127, 127, 127, 127, 127, 127, 255, 255], 
     [255, 255, 127, 127, 127, 127, 127, 127, 255, 255], 
     [255, 255, 255, 255, 255, 255, 255, 255, 255, 255], 
     [255, 255, 255, 255, 255, 255, 255, 255, 255, 255]] 

我幾乎沒有圖像處理的經驗。

回答

1

您應該考慮使用PIL庫:

In [1]: from PIL import Image 
In [2]: img = Image.open('HrWCY.png') 
In [3]: img.getdata().getpixel((0,0)) 
Out[3]: 0 
In [4]: img.getdata().getpixel((4,4)) 
Out[4]: 255 
In [5]: img.getdata().getpixel((5,7)) 
Out[5]: 164 
In [6]: img.getdata().getpixel((12,12)) 
--------------------------------------------------------------------------- 
IndexError        Traceback (most recent call last) 
<ipython-input-6-8456667c785c> in <module>() 
----> 1 img.getdata().getpixel((12,12)) 
IndexError: image index out of range 

檢查documentation,你應該罰款的目的更好的方法。