Nim*_*sal 5 python python-imaging-library python-2.7 python-3.x pillow
from PIL import ImageGrab
pil_img=ImageGrab.grab([0,0,1000,1000])
Run Code Online (Sandbox Code Playgroud)
如果您不对所有像素自己编写循环,速度会快得多。
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))
Run Code Online (Sandbox Code Playgroud)
结果是:
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, ...)
Run Code Online (Sandbox Code Playgroud)
事实上,你有两个以上的像素值是由于 JPG 伪影造成的。您可以编写一些自定义逻辑来查看像素是否更像黑色或红色,并将它们也算作这些。
| 归档时间: |
|
| 查看次数: |
1058 次 |
| 最近记录: |