python - How to check if a PNG has a certain RGB color using Python?
问题描述
I have a .png
file and I want to scan through the image to check if there is a certain RGB value in it. For example, let's say I have an image and want to check if the RGB value (255, 0, 0)
is somewhere in the image. How would I do this in Python? Thanks!
解决方案
我建议您使用PIL-Getpixel或PIL -Getdata
from PIL import Image
im = Image.open('whatever.png').convert("RGB")
# get pixels
pixels = [im.getpixel((i, j)) for j in range(im.height) for i in range(im.width)]
# or
pixels = [i for i in im.getdata()]
#check if tuple of pixel value exists in array-pixel
print((255, 0, 0) in pixels) #True if exists, False if it doesn't
推荐阅读
- python - 从网站生成和下载 tsv(使用 python)
- java - 如何修复 intellij 中阻止我运行项目的错误
- html - 属性:text-decoration-skip-ink 不存在:无
- php - 使用 php (curl) 以编程方式更新 github 代码(无库)
- tensorflow - 针对许多小型矩阵向量乘法优化 Tensorflow
- google-apps-script - Google表格:在选中复选框后防止取消选中该复选框
- html - 如何倾斜带有背景图像的div的底部边框
- excel - 从字符串中删除字符 AZ
- html - 为什么我的网站在移动设备上的布局不同?
- node.js - 无法使用 node.js 将文件转换为 base64 编码