有没有什么方法可以在屏幕上获得最常用的颜色(或最亮的颜色?)作为环境光/显示器背光,而无需过多使用 CPU?有没有办法利用已经计算颜色的显卡(也许是 NVApi)来做到这一点?如果有,有没有办法在 python 的循环中导入该颜色?
我现有的代码有效,但使用了 CPU,这是我在玩更激烈的游戏时需要的。无论如何,这是我现有的代码:
from __future__ import print_function
import binascii
import struct
from PIL import Image
import numpy as np
import scipy
import scipy.misc
import scipy.cluster
import pyautogui
import requests
import json
import sys
class Main():
def __init__(self):
self.OnSwitch = True
while True:
if self.OnSwitch:
self.average_image_color()
self.ColorToLed()
def average_image_color(self):
NUM_CLUSTERS = 5
im = pyautogui.screenshot()
im = im.resize((150, 150)) # optional, to reduce time
ar = np.asarray(im)
shape = ar.shape
ar = ar.reshape(np.product(shape[:2]), shape[2]).astype(float)
codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS)
vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes
counts, bins = np.histogram(vecs, len(codes)) # count occurrences
index_max = np.argmax(counts) # find most frequent
peak = codes[index_max]
color = "#"+binascii.hexlify(bytearray(int(c) for c in peak)).decode('ascii')
self.rgb = self.hex_to_rgb(color)
print(self.rgb)
def hex_to_rgb(self,value):
value = value.lstrip('#')
lv = len(value)
return list(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))
def ColorToLed(self):
shelly = requests.get("http://xxx.xxx.xxx.xx/settings/light/0")
result = json.loads(shelly.content)
if not result["ison"]:
requests.get("http://xxx.xxx.xxx.xx/color/0?turn=on")
requests.get("http://xxx.xxx.xxx.xx/color/0?red="+str(self.rgb[0])+"&&green="+str(self.rgb[1])+"&&blue="+str(self.rgb[2])+"&&gain=100&&white=0&&effect=0&&transition=0")
if __name__ == '__main__':
print("init")
Main()
xxx.xxx.xxx.xx 是我的 RGB LED 控制器的 IP 地址