66import win32con
77import win32gui
88import win32process
9- import cv2
109from PIL import ImageGrab
11- import numpy as np
1210
11+ """
12+ Demo - Save Windows as Images
13+
14+ Works on WINDOWS only.
15+ Saves a window as an image file. Tested saving as PNG and JPG.
16+ saved in the format indicated by the filename.
17+ The window needs to be on the primary display.
18+ 2022 update was to remove OpenCV requirement.
19+
20+ Copyright 2020, 2022 PySimpleGUI.org
21+ """
1322
1423def convert_string_to_tuple (string ):
1524 """
@@ -67,7 +76,6 @@ def enumProcWnds(pid=None):
6776 titles = sorted (titles , key = lambda x : x [0 ].lower ())
6877 return titles
6978
70-
7179def save_win (filename = None , title = None , crop = True ):
7280 """
7381 Saves a window with the title provided as a file using the provided filename.
@@ -82,9 +90,8 @@ def save_win(filename=None, title=None, crop=True):
8290 fceuxHWND = win32gui .FindWindow (None , title )
8391 rect = win32gui .GetWindowRect (fceuxHWND )
8492 rect_cropped = (rect [0 ] + C , rect [1 ], rect [2 ] - C , rect [3 ] - C )
85- frame = np .array (ImageGrab .grab (bbox = rect_cropped ), dtype = np .uint8 )
86- frame = cv2 .cvtColor (frame , cv2 .COLOR_BGR2RGB )
87- cv2 .imwrite (filename , frame )
93+ grab = ImageGrab .grab (bbox = rect_cropped )
94+ grab .save (filename )
8895 sg .cprint ('Wrote image to file:' )
8996 sg .cprint (filename , c = 'white on purple' )
9097 except Exception as e :
0 commit comments