2

我正在开发一个需要操纵整个屏幕上像素的 rgb 颜色的迷你项目。到目前为止我提出的解决方案是连续截屏并通过一些处理找到 rgb 颜色。它足够快,但由于屏幕截图部分,它需要大量的 cpu 使用。

所以,我想知道是否有一种方法可以使用 C++ 库、opengl* 或其他方法从计算机内存或 gpu 内存中提取我需要的数据(以矩阵形式)。

我正在使用 Windows 7 64 位,但如果解决方案是跨平台(windows/linux/mac),我会喜欢它。如果该解决方案可以用 C/C++/Java/Python 实现(这些都可以),那也很好。

*上周我做了一些研究,我认为opengl不可能。不过,我不太确定……

编辑现在我有 Java 和 Python 中的工作代码,可以截取我的整个显示器的屏幕截图并平均像素的 rgb 颜色。在 java 中我使用处理和机器人库,在 python 中我使用 PIL。在 java 中,我使用 createScreenCapture() 抓取图像并使用 getRGB() 读取值。在 python 中,我使用 ImageGrab.grab() 抓取图像,加载对象并读取元组中的值。

作为示例的python代码是这样的:

from PIL import Image
from PIL import ImageGrab
import operator

#######################################
#scree resolution
width = 1920
height = 1080
step = 10
#######################################

def process(pix):
    temp = (0, 0, 0)
    count = 0
    for i in range(0, width, step):
        for j in range(0, height, step):
            temp = tuple(map(operator.add, temp, pix[i,j]))
            count += 1
    return (int(temp[0]/count), int(temp[1]/count), int(temp[2]/count))

while True:
    img=ImageGrab.grab()
    pix = img.load()
    res = process(pix)
    print(res)

但是,此代码仅适用于 Windows(由于 imageGrab)...

4

0 回答 0