2016-08-11 40 views
0

我想將64x64單元格數組轉換爲64x64像素圖像。使用matplotlib和pylab我結束了大約900x900與額外的像素混合在一起的圖像。在Python中直接將數組轉換爲圖像(每個單元格變爲一個像素)

py.figure(1) 
py.clf() 
py.imshow(final_image , cmap='Greys_r') 

如何以1:1的比例將細胞轉換爲像素? (如果你不能說,我對此很新)。

+0

你能提供一個你已經嘗試過的更好的例子嗎? –

+0

單元陣列的內容是什麼? – Karin

回答

0

這是使用PIL創建圖像2x2的示例。一個是大小顏色4(平)陣列

from PIL import Image 

a = [(0, 0, 0), (255, 0, 0), (0, 255, 0), (0, 0, 255)] 

# Create RGB image with size 2x2 
img = Image.new("RGB", (2, 2)) 
# Save it to the new function 
img.putdata(a) 
# Save to the file 
img.save('1.png') 

您應該調整,爲您的數據的格式,如果它是不平坦的,當然。這應該很容易。例如,該腳本從二維列表平展數據:

a = [[[1, 2, 3], [2, 3, 4]], [[5, 6, 7], [8, 9, 10]]] 
a = [tuple(color) for row in a for color in row] 
print a 

如果你正在處理numpy的陣列,而不是名單,你應該使用功能fromarray(通過以下方式):

# data is numpy array 
img = Image.fromarray(data, 'RGB') 
# Save to the file 
img.save('1.png') 

請注意,強烈建議使用numpy數組,因爲它只是包裝C數組,因此它們更快捷。

相關問題