I'm trying to write a small python script that will grab a part of the screen, check if it contains a pixel of a certain color and if yes, use the 'keyboard' package to write 'foo' and click enter.
My problem is this: to use 'keyboard', the script must be run as root but doing this somehow breaks the screen-grab part (if I check it with im.show(), it's all black), so the script just loops and sleeps without doing anything.
These are the relevant snippets of my code:
from time import sleep
import keyboard
import pyscreenshot as psc
while True:
useKB = False
im = psc.grab(bbox=(x0,y0,x1,y1)) #grab screen area
rgb_im = im.convert('RGB') #convert image to rgb
for y in range(0,y1-y0):
for x in range(0,x1-x0):
r,g,b = rgb_im.getpixel((x,y))
if r==3 and g==251 and b==73: #found pixel
useKB = True
break;
if useKB == True:
break
if useKB == True:
#if I comment this 2 lines out and run without sudo, the script works as intended.
keyboard.write('foo')
keyboard.press_and_release('enter')
sleep(1)
Thank you all.
question from:
https://stackoverflow.com/questions/66055914/run-pyscreenshot-as-root 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…