简体   繁体   中英

How to capture screenshot in Python with lackey?

I would like to do a screenshot with lackey of ideally the screen of an app (but to begin with, a screenshot of the whole screen would be OK).

I have tried

from lackey import *

notepad = App('notepad.exe')
notepad.open()
focusWindow = notepad.focusedWindow()

s = Screen(0)
r = s.capture()
with open("toto.bmp", "wb") as f:
    f.write(r)

The picture cannot be open because the function capture returns a numpy.ndarray .

I also tried to do the following but the result is the same:

r = Screen.capture(focusWindow)

Anyone knows how to do a screenshot?

Thanks

You can use the Image.fromarray and Image.save methods from the PIL library to save the image. For some reason the code below captures the window running the script as well as the notepad app, sp I guess you might have to tweak it.

from lackey import *
from PIL import Image

notepad = App('notepad.exe')
notepad.open()
focusWindow = notepad.focusedWindow()

sleep(5) # allow some time for the notepad window to appear before capture.

screen = Screen()
capture = screen.capture(focusWindow)

image = Image.fromarray(capture)
image.save("test.bmp")
notepad.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM