• Version: 20261019
  • Source Code Copyright: Unknown
  • Redistribution Restriction: This source code is integrated by LEADERG INC. Please use it privately. Please DO NOT distribute it to internet.

Spyder IDE project for Windows that captures one specific window (Notepad) to a JPEG file. main.py finds the window with win32gui.FindWindow, reads its rectangle with GetWindowRect, copies the window's pixels into a bitmap with the Win32 GDI calls of pywin32 (GetWindowDC, CreateCompatibleDC, CreateCompatibleBitmap, BitBlt), converts the bitmap into a NumPy array and saves it as screenshot.jpg (JPEG quality 100) with OpenCV. It prints the window coordinates and the elapsed time.

 

[Folder Structure]

  • .spyproject/: Spyder project settings.
  • copyright.txt: Copyright notice.
  • IO-WindowCapture-Spyder.html: This document (usage page).
  • main.py: The sample: captures the Notepad window and saves it as screenshot.jpg.
  • screenshot.jpg: Sample output (890 x 510 JPEG of a Notepad window). main.py overwrites it when run from this folder.

[Prerequisites]

  • Windows: the program uses the Win32 API through pywin32 (win32gui, win32ui, win32con, win32api).
  • Python 3 with the Spyder IDE.
  • Python packages imported by main.py, installed into the environment that Spyder uses: pip install pywin32 opencv-python numpy
  • Windows display scaling set to 100% (see Notes).

[How to Run]

Spyder

  1. Open Notepad so that the window to capture exists.
  2. Start Spyder and choose Projects > Open Project..., then select this folder (IO-WindowCapture-Spyder-{version}).
  3. Open main.py from the project tree and choose Run > Run (F5).
  4. The console shows left =, top =, right =, bot =, w =, h = and time (sec) =, and the capture is written to screenshot.jpg in the working directory (this folder when Spyder runs the file from its own folder), replacing the sample file.
  5. If no matching window is found, the program prints Specific Window is not found! and exits without writing a file.

 

Command line

From this folder, with Notepad open:

python main.py

[Sample Output]

The sample covers the whole window rectangle returned by GetWindowRect, including the menu bar.

[Notes]

  • Display scaling: the comment in main.py states that the screen display ratio must be 100% and only 100%.
  • Choosing the window: main.py calls window_capture('NotePad', None). Inside window_capture only the first parameter (window_name) is used: the function runs win32gui.FindWindow(window_name, None) and ignores its second parameter (process_name). The first argument of FindWindow is the window class name and the second is the window title, so the lookup is by the Notepad window class. To capture another application, change the class name in the window_capture(...) call, or edit the FindWindow line inside window_capture to look the window up by its title.
  • The result of cv2.cvtColor(image, cv2.COLOR_BGRA2RGB) is not assigned, so that line has no effect; the 4-channel array read from the bitmap (image.shape = (h, w, 4)) is passed to cv2.imwrite as is. The commented-out lines in main.py show how to save a PNG instead or display the image with cv2.imshow.
  • IO-WindowCapture-PyQT-GPL-Spyder is a sister sample that finds the same window with win32gui but grabs it with PyQt5 (QScreen.grabWindow) and saves a PNG.

[Version History]

  • 20261019: The folder was renamed from IO-WindowCapture-Spyder-2 to IO-WindowCapture-Spyder-20261019 (the version is now the date of the last change, YYYYMMDD). This usage page was added and the version history moved here from version.txt, which was removed.
  • 2: Add error handling.
  • 1: Initial version.

 

This SDK is built in AppForAI - AI Dev Tools.