本人想用Python 做一個像素RGB識別功能,于是找到了PyautoGUI模塊包。
獲取滑鼠當前位置RGB函式如下:
try:
while True:
# TODO: Get and print the mouse coordinates.
x,y = pyautogui.position()
positionStr = 'X:'+ str(x).rjust(4) + ' Y:' + str(y).rjust(4) #rjust() 字串方法將對坐標右調整,讓它們占據同樣的寬度.
pixelColor = pyautogui.screenshot().getpixel((x,y))
positionStr += ' RGB: (' + str(pixelColor[0]).rjust(3)
positionStr += ', ' + str(pixelColor[1]).rjust(3)
positionStr += ', ' + str(pixelColor[2]).rjust(3) + ')'
print(positionStr,end='')
print('\b'*len(positionStr),end='',flush=True)
print(pixelColor)
except KeyboardInterrupt: # 當用戶按下 Ctrl-C,程式執行將轉到 except 子句
print('\ndone')
1.代碼運行正常,但是得到的RGB值是不對的,原因是我打開MAC中的數碼測色器對比,我發現獲得的滑鼠當前位置的RGB值和系統自帶的數碼測色器獲得的RGB值不同,而且差別很大,完全不一樣。
2.通過在螢屏上把滑鼠放在不同的顏色塊上,Python得到的RGB數值有一樣的情況。
所以得出的結論是,通過Python得到的RGB值是錯誤的,于是我做出以下判斷:
1.滑鼠的坐標獲取有問題,我用其他的代碼獲取滑鼠實時坐標,發現滑鼠坐標的獲取是沒問題的
2.螢屏解析度的問題,我更改了螢屏不同的解析度,并沒有任何改善
根據以上提問,請求大神們指點,是哪里出了問題,是不是還需要引入其他模塊或者少了什么東西
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/52695.html
