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))
如果不在所有像素上自己写一个循环,速度会快得多。在
结果是:
^{pr2}$事实上,你有两个以上的像素值是由于JPG人工制品。您可以编写一些自定义逻辑来查看像素是否更像黑色或红色,并将它们也计算在内。在
使用
pil_img.getpixel((x, y))
,或pil_img[x, y]
。在相关问题 更多 >
编程相关推荐