python中实时查看鼠标所指的像素点的RBG数值

import time
import pyautogui


def rgb2hex(r, g, b):
    return '#{:02x}{:02x}{:02x}'.format(r, g, b)


try:
    width, height = pyautogui.size()
    while True:
        time.sleep(1)
        x, y = pyautogui.position()
        rgb = pyautogui.screenshot().getpixel((x, y))
        r = str(rgb[0]).rjust(3)
        g = str(rgb[1]).rjust(3)
        b = str(rgb[2]).rjust(3)
        hex_c = rgb2hex(int(r), int(g), int(b))
        color_str = f'选中颜色   RGB:({r},{g},{b}),  16进制:{hex_c}'
        print(color_str)
except KeyboardInterrupt:
    exit('\n\n---- Bye.\n')

  

posted @ 2022-09-29 17:19  小二君i  阅读(149)  评论(0)    收藏  举报