Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
67 views
in Technique[技术] by (71.8m points)

python - Run pyscreenshot as root

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...