Removed need for OpenCV and numpy.
This commit is contained in:
parent
5b3c3e5375
commit
223cd18964
|
@ -1,8 +1,6 @@
|
||||||
import PySimpleGUI as sg
|
import PySimpleGUI as sg
|
||||||
import win32gui
|
import win32gui
|
||||||
import cv2
|
|
||||||
from PIL import ImageGrab
|
from PIL import ImageGrab
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
Demo - Save Window screenshot
|
Demo - Save Window screenshot
|
||||||
|
@ -10,7 +8,7 @@ import numpy as np
|
||||||
Saves a window as an image file. Tested saving as PNG and JPG. Input the title of the Window and it will be
|
Saves a window as an image file. Tested saving as PNG and JPG. Input the title of the Window and it will be
|
||||||
saved in the format indicated by the filename.
|
saved in the format indicated by the filename.
|
||||||
|
|
||||||
Copyright 2020 PySimpleGUI.org
|
Copyright 2020, 2022 PySimpleGUI.org
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# --------------------------------- Function to Save Window as JPG ---------------------------------
|
# --------------------------------- Function to Save Window as JPG ---------------------------------
|
||||||
|
@ -41,9 +39,8 @@ def save_win(filename=None, title=None):
|
||||||
fceuxHWND = win32gui.FindWindow(None, title)
|
fceuxHWND = win32gui.FindWindow(None, title)
|
||||||
rect = win32gui.GetWindowRect(fceuxHWND)
|
rect = win32gui.GetWindowRect(fceuxHWND)
|
||||||
rect_cropped = (rect[0]+C, rect[1], rect[2]-C, rect[3]-C)
|
rect_cropped = (rect[0]+C, rect[1], rect[2]-C, rect[3]-C)
|
||||||
frame = np.array(ImageGrab.grab(bbox=rect_cropped), dtype=np.uint8)
|
grab = ImageGrab.grab(bbox=rect_cropped)
|
||||||
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
grab.save(filename)
|
||||||
cv2.imwrite(filename, frame)
|
|
||||||
sg.popup('Wrote image to file:',filename)
|
sg.popup('Wrote image to file:',filename)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
sg.popup('Error trying to save screenshot file', e)
|
sg.popup('Error trying to save screenshot file', e)
|
||||||
|
|
|
@ -6,10 +6,19 @@ import win32api
|
||||||
import win32con
|
import win32con
|
||||||
import win32gui
|
import win32gui
|
||||||
import win32process
|
import win32process
|
||||||
import cv2
|
|
||||||
from PIL import ImageGrab
|
from PIL import ImageGrab
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
Demo - Save Windows as Images
|
||||||
|
|
||||||
|
Works on WINDOWS only.
|
||||||
|
Saves a window as an image file. Tested saving as PNG and JPG.
|
||||||
|
saved in the format indicated by the filename.
|
||||||
|
The window needs to be on the primary display.
|
||||||
|
2022 update was to remove OpenCV requirement.
|
||||||
|
|
||||||
|
Copyright 2020, 2022 PySimpleGUI.org
|
||||||
|
"""
|
||||||
|
|
||||||
def convert_string_to_tuple(string):
|
def convert_string_to_tuple(string):
|
||||||
"""
|
"""
|
||||||
|
@ -67,7 +76,6 @@ def get_window_list():
|
||||||
titles = sorted(titles, key=lambda x: x[0].lower())
|
titles = sorted(titles, key=lambda x: x[0].lower())
|
||||||
return titles
|
return titles
|
||||||
|
|
||||||
|
|
||||||
def save_win(filename=None, title=None, crop=True):
|
def save_win(filename=None, title=None, crop=True):
|
||||||
"""
|
"""
|
||||||
Saves a window with the title provided as a file using the provided filename.
|
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):
|
||||||
fceuxHWND = win32gui.FindWindow(None, title)
|
fceuxHWND = win32gui.FindWindow(None, title)
|
||||||
rect = win32gui.GetWindowRect(fceuxHWND)
|
rect = win32gui.GetWindowRect(fceuxHWND)
|
||||||
rect_cropped = (rect[0] + C, rect[1], rect[2] - C, rect[3] - C)
|
rect_cropped = (rect[0] + C, rect[1], rect[2] - C, rect[3] - C)
|
||||||
frame = np.array(ImageGrab.grab(bbox=rect_cropped), dtype=np.uint8)
|
grab = ImageGrab.grab(bbox=rect_cropped)
|
||||||
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
grab.save(filename)
|
||||||
cv2.imwrite(filename, frame)
|
|
||||||
sg.cprint('Wrote image to file:')
|
sg.cprint('Wrote image to file:')
|
||||||
sg.cprint(filename, c='white on purple')
|
sg.cprint(filename, c='white on purple')
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
Loading…
Reference in New Issue