0

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

Jean-Francois T.
  • 11,549
  • 7
  • 68
  • 107

1 Answers1

1

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()
jpw
  • 44,361
  • 6
  • 66
  • 86
  • 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 '18 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 '18 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 03 '18 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 04 '18 at 01:51