Python Forum
Screenshot of specific window
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Screenshot of specific window
#1
Hi,

I have a program that needs to take an image of a small section of another program at 29.97 fps. Currently I'm using mss and manually specifying coordinates on the screen. I'm trying to figure out a way to do this automatically, preferably not needing the window to be active/at the front of the screen, i.e could have another window covering it.

I've searched extensively on the internet however I haven't found anything useful so far. I honestly have no idea where to start.

Thanks
Reply
#2
Hello,

What is your OS?

For windows, you can find the position and the size of a window with libraries like pywinauto or pywin32. Once you have this, you can take a screenshot with pillow.

Best Regards,

Avorane
Reply
#3
Hey,

Thanks for the reply!

This is definitely an option, however that requires the windows to be visible on the screen, ideally I'd be able to screenshot the window even while it's covered by something else (not minimized, just behind something)
Reply
#4
Hello,

With the library pywin32, you can. When you get the identifier of the window (for example by the name) you can set it to the foreground with the function SetForegroundWindow.

Here, an example: https://www.blog.pythonlibrary.org/2014/...-to-front/

And after, take a screenshot with PIL (pillow) when you get position and size.

And you have a powerful library for windows application with python: pywinauto. With it, you can control a window (but only if use the spec win32 or uia), get position and size and more. Look here: https://pywinauto.readthedocs.io/en/late...e_as_image

Best Regards,

Avorane
Reply
#5
If I use pywin32 to set it to the foreground though that just means it'll pop in front of all the other windows, the idea is to be able to capture the windows contents when it's behind other windows. So far my solution is using win32 to get the window location and bounding box and am then taking a screenshot with mss, but that requires the window be in the foreground. I would still really like to find a solution that lets the window be in the background
Reply
#6
Hello,

Sorry for my bad comprehension.

You can find a solution here: https://stackoverflow.com/questions/1969...w-win32gui

Best Regards,

Nicolas TATARENKO
Reply
#7
Thanks for the response!

I've attempted to use that however all it gives is black screenshots, looking in the comments it appears someone else had the same problem. The code was originally written for python 2.7, I'm using 3.6 at the moment. Would you or anyone have an idea on potential fixes? I'm looking through it now but it's a bit over my head.
Reply
#8
Hello,

For pywin32, have you executed the script [dir_of_python]\Scripts\pywin32_postinstall.py with the command "python pywin32_postinstall.py -install"?

Sorry, but i have not windows and so cannot test the scripts with Windows.

Best Regards,

Nicolas TATARENKO

Hello,

An other test (because i had the problem on a windows to work), you can execute a right-clik on python.exe and select the option ""Disable DPI scaling".

Best Regards,

Nicolas TATARENKO
Reply
#9
Yeah I've run the post install, I can't find any option to disable DPI scaling, however I tried to do it in my program with:

ctypes.windll.shcore.SetProcessDpiAwareness(False)
Reply
#10
Hello,

We have tested this method on a project, but it dosen't work for we.

And what is the result for you?

The solution was the DPI option on the python executable.

Normally, you can find it in the python directory, right-click on python.exe. In the tab Compatibility, check the checkbox "Disable display scaling on high DPI settings". Or, for windows 10 i think, you can set the combobox "High DPI scaling override" to "program".

And for test, check that the windows scaling is to 100%.

Best Regards,

Nicolas TATARENKO
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a way to call and focus any popup window outside of the main window app? Valjean 6 1,777 Oct-02-2023, 04:11 PM
Last Post: deanhystad
  Pyspark Window: perform sum over a window with specific conditions Shena76 0 1,177 Jun-13-2022, 08:59 AM
Last Post: Shena76
  IEDriverServer screenshot ABVSVL 0 1,733 Jul-12-2020, 09:31 AM
Last Post: ABVSVL
  pyautogui.screenshot region is not working alexlu 6 6,509 Jun-04-2020, 10:46 AM
Last Post: alexlu
  What is the best way to search for a pixel in a screenshot? TheZadok42 1 2,600 May-15-2020, 12:37 PM
Last Post: scidam
  Delete specific lines contain specific words mannyi 2 4,138 Nov-04-2019, 04:50 PM
Last Post: mannyi
  Want to take a Screenshot from a File in Linux dhiliptcs 2 2,563 Oct-21-2019, 01:22 PM
Last Post: dhiliptcs
  datetime with every screenshot name evilcode1 7 5,527 Aug-27-2018, 06:01 PM
Last Post: buran
Question [Help] Convert integer to Roman numerals? {Screenshot attached} vanicci 10 9,184 Aug-06-2018, 05:19 PM
Last Post: vanicci
  [Help] sorted() in while loop with user's input() {Screenshot attached} vanicci 5 4,017 Aug-04-2018, 08:59 PM
Last Post: vanicci

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020