Python,  制作

Python-屏幕取色器

学了PyAutoGUI和pynput模块后,突发奇想,想做一个简单的屏幕取色器,于是出了这篇文章

功能实现:
实现对鼠标指针,指定对像素点进行RGB颜色取值,并将数值自动复制到电脑剪切板中,Ctrl+v即可将值取出
from pynput.mouse import Button, Controller,Listener
from pynput.keyboard import Key, Controller
from pynput import keyboard
import pyautogui as pag
import pyautogui
import pyperclip

def on_click(x, y, key, pressed):
	# print('{0} at {1}'.format('按下' if pressed else '松开',(x, y)))
	if key == Button.right:
		img=pag.screenshot()
		(r,g,b) = img.getpixel((x, y))
		print('R =',r, 'G =',g, 'B =',b)
		color = '('+str(r)+' '+str(g)+' '+str(b)+')'
		pyperclip.copy(color)
		if not pressed:
			# 停止监听
			return False
	else:
		pass

def on_press(key):
	print(pyautogui.keyDown('y'))
	return key

while True:
	with Listener(on_click = on_click, on_press = on_press) as listener:
		listener.join()

 

留言