感谢阅读。
我有一个带有功能的小型应用程序,该功能将从配置文件中获取信息。例如 K:8 ,然后检查该键是否按下。此外,它将使用 P:255.255.255.0010.0010 然后 split(“。”)转换为int并检查在10X位置是否存在RGB 255,255,255的像素10年。
这是一个循环,它似乎可以正常工作,检查大约需要0.2秒。这足够快满足我的需求,但是我确实需要每秒检查几下的解决方案。我的问题是,循环执行此操作会导致显示滞后。好像显示降低到大约2 FPS。因此,想知道是否有人会知道为什么会发生这种情况,或者我能做些什么来解决它,同时仍能获得0.3s以内的像素检查?
这是我的职能:
def conditionCheck():
if DataHeader[0][0][:2] == "B:": #Box limits in a string, split and convert to int.
bbox = DataHeader[0][0][2:]
bbox = bbox.split(".")
bboxx1 = int(bbox[0])
bboxy1 = int(bbox[1])
bboxx2 = int(bbox[2])
bboxy2 = int(bbox[3])
pixel = (pyscreenshot.grab(bbox=(bboxx1, bboxy1, bboxx2, bboxy2))).load() #Grab screen, within limits.
cp = 0
while cp < len(DataConditions): #cycle through config file list
rp = 0
conditionPass = 0
while rp < len(DataConditions[cp]): #Cycle through list item's conditions.
if DataConditions[cp][rp][:2] == "K:":
if keyboard.is_pressed(DataConditions[cp][rp][2:]): #Check if key down, set pass to true
conditionPass = 1
rp += 1 #Move to check next condition.
print("PRESSED") #DEBUG
continue
conditionPass = 0
break
if DataConditions[cp][rp][:2] == "P:": #pixel colour check, match = true
pixelData = DataConditions[cp][rp][2:] #pixel data and location in one string, conv to int and split.
pixelData = pixelData.split(".")
pixelLocation = pixel[int(pixelData[3]), int(pixelData[4])]
pixelData = pixelData[0], pixelData[1], pixelData[2]
if pixelLocation == tuple(map(int, pixelData)):
print("PASSED PIXEL") #DEBUG
conditionPass = 1
rp += 1
continue
conditionPass = 0
break
rp += 1
if conditionPass == 1:
return cp #end loop, send line of true condition (in config file)
cp += 1
return 0 #Return 0 if nothing found.
我正在 Linux 上运行此程序,因此我需要一个跨平台/ Linux友好的解决方案。
思路:我有点菜鸟,请随时就其他方面提供建议。 我知道在C中执行此操作会获得更快,更流畅的结果,但是应用C的其他部分将很难在C中实现(而且我的知识有限),所以我尝试看看是否可以在Python中实现,但是也许有太多的开销吗?
谢谢
答案 0 :(得分:0)
一段时间后,我认为我已经能够使用其他插件但相同的方法来找到解决方案。因此:
def conditionCheck():
if DataHeader[0][0][:2] == "B:": #Box limits in a string, split and convert to int.
bbox = DataHeader[0][0][2:]
bbox = bbox.split(".")
#bboxx1 = int(bbox[0])
#bboxy1 = int(bbox[1])
#bboxx2 = int(bbox[2])
#bboxy2 = int(bbox[3])
monitorWindow = {"top": int(bbox[0]), "left": int(bbox[1]), "width": int(bbox[2]), "height": int(bbox[3])}
with mss.mss() as sct: #Grab screen, within limits.
sct_img = sct.grab(monitorWindow)
img = Image.new("RGB", sct_img.size)
pixels = img.load()
for x in range(sct_img.width):
for y in range(sct_img.height):
pixels[x, y] = sct_img.pixel(x, y)
cp = 0
while cp < len(DataConditions): #cycle through config file list
rp = 0
conditionPass = 0
while rp < len(DataConditions[cp]): #Cycle through list item's conditions.
if DataConditions[cp][rp][:2] == "K:":
if keyboard.is_pressed(DataConditions[cp][rp][2:]): #Check if key down, set pass to true
conditionPass = 1
rp += 1 #Move to check next condition.
print("PRESSED") #DEBUG
continue
conditionPass = 0
break
if DataConditions[cp][rp][:2] == "P:": #pixel colour check, match = true
pixelData = DataConditions[cp][rp][2:] #pixel data and location in one string, conv to int and split.
pixelData = pixelData.split(".")
pixelLocation = pixels[int(pixelData[3]), int(pixelData[4])]
pixelData = pixelData[0], pixelData[1], pixelData[2]
if pixelLocation == tuple(map(int, pixelData)):
print("PASSED PIXEL") #DEBUG
conditionPass = 1
rp += 1
continue
conditionPass = 0
break
rp += 1
if conditionPass == 1:
return cp #end loop, send line of true condition (in config file)
cp += 1
return 0 #Return 0 if nothing found.