您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python – 如何在PIL图像中得不到红色像素和黑色像素

5b51 2022/1/14 8:21:38 python 字数 1747 阅读 521 来源 www.jb51.cc/python

from PIL import ImageGrab pil_img=ImageGrab.grab([0,0,1000,1000]) 现在我想在两个单独的变量中没有红色像素和黑色像素.所以我应该如何继续pil_img最佳答案如果你不在所有像素上自己编写循环,它会快得多.import os.path from collections import Counter

概述

from PIL import ImageGrab
pil_img=ImageGrab.grab([0,1000,1000])

现在我想在两个单独的变量中没有红色像素和黑色像素.所以我应该如何继续pil_img

The image look likes this

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): 735408,(248,11): 8700,(245,9,9): 7200,...)
Counter({(0,0): 5992,9): 1639,(3,0): 33,6,0): 23,...)

您有两个以上像素值的事实是由JPG伪影造成的.您可以编写一些自定义逻辑来查看像素是否更像黑色或红色,并将它们计算为那些.

总结

以上是编程之家为你收集整理的python – 如何在PIL图像中得不到红色像素和黑色像素全部内容,希望文章能够帮你解决python – 如何在PIL图像中得不到红色像素和黑色像素所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶