Skip to content

Commit 223cd18

Browse files
committed
Removed need for OpenCV and numpy.
1 parent 5b3c3e5 commit 223cd18

2 files changed

Lines changed: 16 additions & 12 deletions

File tree

DemoPrograms/Demo_Save_Any_Window_As_Image.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import PySimpleGUI as sg
22
import win32gui
3-
import cv2
43
from PIL import ImageGrab
5-
import numpy as np
64

75
"""
86
Demo - Save Window screenshot
97
Works on WINDOWS only.
108
Saves a window as an image file. Tested saving as PNG and JPG. Input the title of the Window and it will be
119
saved in the format indicated by the filename.
1210
13-
Copyright 2020 PySimpleGUI.org
11+
Copyright 2020, 2022 PySimpleGUI.org
1412
"""
1513

1614
# --------------------------------- Function to Save Window as JPG ---------------------------------
@@ -41,9 +39,8 @@ def save_win(filename=None, title=None):
4139
fceuxHWND = win32gui.FindWindow(None, title)
4240
rect = win32gui.GetWindowRect(fceuxHWND)
4341
rect_cropped = (rect[0]+C, rect[1], rect[2]-C, rect[3]-C)
44-
frame = np.array(ImageGrab.grab(bbox=rect_cropped), dtype=np.uint8)
45-
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
46-
cv2.imwrite(filename, frame)
42+
grab = ImageGrab.grab(bbox=rect_cropped)
43+
grab.save(filename)
4744
sg.popup('Wrote image to file:',filename)
4845
except Exception as e:
4946
sg.popup('Error trying to save screenshot file', e)

DemoPrograms/Demo_Save_Windows_As_Images.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,19 @@
66
import win32con
77
import win32gui
88
import win32process
9-
import cv2
109
from 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

1423
def 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-
7179
def 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

Comments
 (0)