Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Taking a print screen of a particular window using PIL using Python

I want to take a print screen of a particular window on my PC by running the python program and it taking that screen shot, before cropping it and comparing its hex value to other hex values in a SQL server.

I have thought about letting it wait 10 seconds whilst I get the other window up and then start taking the print screens continuously until one matches.

I was just wondering if I can use python to maximize that particular window that I want to print screen automatically and then have it take a print screen.

If not, could I take a print screen (a picture) of a window that is minimized? (I think this is impossible)..

Thanks!

like image 367
Tom Pitts Avatar asked May 19 '26 03:05

Tom Pitts


1 Answers

If you do a quick Google search, you will find this helpful tutorial that uses PIL to take a screenshot of a desktop:

  • http://www.blendedtechnologies.com/quick-screenshots-script-python-pil/38

Assuming you're on Windows, you would need to use PyWin32 (or possibly pywinauto) to get the window you want. These two links will help with that:

  • Get HWND of each Window Python
  • http://timgolden.me.uk/python/win32_how_do_i/find-the-window-for-my-subprocess.html

Then you could use MoveWindow to resize the window you found before taking the screenshot:

  • How can I get the window focused on Windows and re-size it?
like image 52
Mike Driscoll Avatar answered May 21 '26 15:05

Mike Driscoll