文章背景:最近在学习pyautogui模块中的处理屏幕,想要通过运行python代码,获取鼠标光标处像素的RGB颜色值。python代码如下:
# from the book: Automate the Boring Stuff with Pythonimport pyautoguipyautogui.pause = 1pyautogui.failsafe = Trueprint('Press Ctrl-C to quit.')try: while True: # Get and print the mouse coordinates. x, y = pyautogui.position() positionStr = 'X: ' + str(x).rjust(4) + ' Y: ' + str(y).rjust(4) 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(