1
我正在寫這個代碼獲取圖像的所有像素,然後將它們存儲到列表中。將一個循環轉換爲一個列表理解
我的目標是將此循環轉換爲列表理解。
# Take the image
im = Image.open("C:/Users/admin/Desktop/IMG_170619_114715_0007_NIR.tif")
#load it
pixels = im.load()
# take the width and the height
width, height = im.size
然後是環......
all_pixels = []
for x in range(width):
for y in range(height):
cpixel = pixels[x,y]
all_pixels.append(cpixel)
我第一次嘗試是這樣的......
all_pixels=[[pixels[x,y] for y in range(height)] for x in range(width)]
而且它的工作,但是當我做LEN(all_pixels)看看我是否有所有的像素,我只有1280個值,這意味着計算沒有正確完成在lsit理解...
len(all_pixels) Out[143]: 1280
我們應該有這樣的事情..
len(all_pixels) Out[105]: 1228800
任何幫助嗎?爲什麼列表理解不會計算循環的相同內容?
頭腦不'像素[X] [Y]'... –
@WillemVanOnsem我不認爲這有什麼差別。 –
謝謝先生!這有助於.. –