python - 从屏幕截图中获取特定 RGB 值的 x,y 坐标列表
问题描述
我一直在尝试截取屏幕截图并找到特定颜色的每个 x,y 坐标。
from PIL import ImageGrab
import numpy as np
image = ImageGrab.grab()
indices = np.all(image == (209, 219, 221), axis=-1)
print(indices)
print(zip(indices[0], indices[1]))
当我运行我的代码时,我收到一个坐标,然后收到一条错误消息。
(1126, 555)
[1126, 555]
False
print(zip(indices[0], indices[1]))
IndexError: invalid index to scalar variable.
它怎么不起作用?颜色在屏幕上。
解决方案
我相信您在以下行中犯了错误:
indices = np.all(image == (209, 219, 221), axis=-1)
您可以直接遍历像素并获得您想要的结果:
from PIL import ImageGrab
import numpy as np
image = ImageGrab.grab()
color = (43, 43, 43)
indices = []
width, height = image.size
for x in range(width):
for y in range(height):
if image.getpixel((x, y)) == color:
indices.append((x, y))
print(indices)
推荐阅读
- symlink - 找到损坏的符号链接,然后在备份中找到相应的文件并重新链接
- javascript - 迭代嵌套数据 JS
- typescript - eslinting 问题和我的打字稿反应应用程序正在崩溃
- azure-devops - 在 Azure DevOps 中捕获每个拉取请求的提交计数
- php - 单击href时如何在php中保留变量的值?
- google-maps - Jasper 报告 - 将 Google 地图导出为 PDF 时的标记限制
- ios - iOS 13.5.1 和 UIButton 渐变
- windows - Bash 脚本无法运行 - Word 意外和意外标记
- java - 在 JAVA 中使用 Regex 进行子序列匹配
- three.js - 当前如何在three.js场景中获取相机的旋转?