2017-09-13 149 views

回答

1

它,如果你不寫一個循環自己所有像素快得多。

import os.path 
from collections import Counter 

from PIL import Image 

path_to_file = os.path.join('..', '..', 'img', '9BLW9.jpg') 

# Count the number of occurrences per pixel value for the entire image 
img = Image.open(path_to_file) 
pixels = img.getdata() 
print(Counter(pixels)) 

# Count the number of occurrences per pixel value for a subimage in the image 
img = img.crop((100, 100, 200, 200)) 
pixels = img.getdata() 
print(Counter(pixels)) 

有了結果:

Counter({(248, 8, 9): 1002251, (0, 0, 0): 735408, (248, 8, 11): 8700, (245, 9, 9): 7200, ...) 
Counter({(0, 0, 0): 5992, (248, 8, 9): 1639, (3, 0, 0): 33, (0, 6, 0): 23, ...) 

,你有兩個以上的像素值的事實是,由於JPG文物。您可以編寫一些自定義邏輯,以查看像素是否更像黑色或紅色,並將它們計算爲這些像素。

+0

感謝這個還曾:) –

+0

你能告訴我你是怎麼能這將是感激看到我question.Initially它有不超過10至15次 –

+0

我過濾與標籤Python的圖片庫問題,並看到你的問題在列表中向下滾動不會太多了。 – physicalattraction

2

使用pil_img.getpixel((x, y)),或pil_img[x, y]

+0

Thanks.It工作。 –

+0

如果您給予好評的question.I失去了我的previlege要問的問題。:-( –

+0

並請編輯您的函數接受一個元組:) –

相關問題