添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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()
                Nice!! Follow-up question: your example is doing a screenshot of the region of the batch (I'm running from MSYS) instead of "notepad.exe". What should be changed to have the screenshot of the correction region?  Thanks ALOT!
– Jean-Francois T.
                Aug 31, 2018 at 14:08
                @Jean-FrancoisT. I noticed that too, but I couldn't find the reason. I guess it has to do with how you get the region of the App window.
– jpw
                Aug 31, 2018 at 14:13
                @Jean-FrancoisT. I've played around with different methods in the App, Screen and Region classes of Lackey, but no matter what I've tried i can't seem to get only the Notepad window without also getting the window running the code (VS Code in my case).
– jpw
                Sep 3, 2018 at 11:41
                Strange... Do you think it is a bug? For the need of switching, probably pywin32 can help switching to the window and get the proper window region. Anyway, I might not need this functionality actually. It was rather for testing the different functionalities.
– Jean-Francois T.
                Sep 4, 2018 at 1:51
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.