I want to capture the screenshot of the existing running window via Python.
Following is my sample code for capture Notepad window:
import time
import win32gui
import win32ui
import win32con
import pyautogui
def getWindowScreenshot(hwnd, output_file):
r = win32gui.GetWindowRect(hwnd)
width = r[2] - r[0]
height = r[3] - r[1]
wDC = win32gui.GetWindowDC(hwnd)
dcObj = win32ui.CreateDCFromHandle(wDC)
cDC = dcObj.CreateCompatibleDC()
dataBitMap = win32ui.CreateBitmap()
dataBitMap.CreateCompatibleBitmap(dcObj, width, height)
cDC.SelectObject(dataBitMap)
cDC.BitBlt((0, 0), (width, height), dcObj, (0, 0), win32con.SRCCOPY)
# save the bitmap to a file
dataBitMap.SaveBitmapFile(cDC, output_file)
dcObj.DeleteDC()
cDC.DeleteDC()
win32gui.ReleaseDC(hwnd, wDC)
win32gui.DeleteObject(dataBitMap.GetHandle())
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
window_title = "Untitled - Notepad"
winHandle = win32gui.FindWindow(None, window_title)
if winHandle == 0:
raise RuntimeError(f"Window '{window_title}' is not opening!")
# Bring the window to foreground in case it is overlap by other windows
win32gui.SetForegroundWindow(winHandle)
# Do something with Notepad
time.sleep(3)
# Capture the window
getWindowScreenshot(winHandle, "output.bmp")
However, sometimes the captured image will have the blinking caret indicator on the input textbox.
My question is, how to capture the screenshot without getting the caret indicator?
I already tried some solution, but not suitable:
Solution 1:
# Bring the window to foreground in case it is overlap by other windows
win32gui.SetForegroundWindow(winHandle)
# Do something with Notepad
time.sleep(3)
# Make the "Desktop window" focus
win32gui.SetForegroundWindow(win32gui.GetDesktopWindow())
# Capture the window
getWindowScreenshot(winHandle, "output.bmp")
Before capturing the screenshot, try to set "Desktop" as the foreground window.
However, the command win32gui.SetForegroundWindow(win32gui.GetDesktopWindow()) will raise an error:
pywintypes.error: (0, 'SetForegroundWindow', 'No error message is available')
Solution 2:
Replace win32gui.SetForegroundWindow(win32gui.GetDesktopWindow()) with win32gui.ShowWindow(winHandle, 4), where 4 is SW_SHOWNOACTIVATE.
The code run OK, but the Notepad window doesn't lose focus after execution.
Solution 3:
# Bring the window to foreground in case it is overlap by other windows
win32gui.SetForegroundWindow(winHandle)
# Do something with Notepad
time.sleep(3)
# Click outside of window
r = win32gui.GetWindowRect(winHandle)
pyautogui.click(r[2]+1, r[3]+1)
# Capture the window
getWindowScreenshot(winHandle, "output.bmp")
With this solution, I tried to click outside the Notepad window (click on the desktop). This code seems worked, but will not work if another window is behind the Notepad window like this:
When the code is executed, it will click on the Windows Explorer window. So, the code can perform clicking on something unexpectedly (example: deleting the system file unexpectedly?).
Solution 4
(from #SongZhu-MSFT)
if __name__ == '__main__':
window_title = "Untitled - Notepad"
winHandle = win32gui.FindWindow(None, window_title)
if winHandle == 0:
raise RuntimeError(f"Window '{window_title}' is not opening!")
# Bring the window to foreground in case it is overlap by other windows
win32gui.SetForegroundWindow(winHandle)
# Do something with Notepad
time.sleep(3)
win32gui.EnableWindow(winHandle,False)
# Capture the window
getWindowScreenshot(winHandle, "output.bmp")
time.sleep(0.5)
getWindowScreenshot(winHandle, "output1.bmp")
time.sleep(5)
win32gui.EnableWindow(winHandle,True)
When I run the code, I still see that the caret displaying all time.
And output.bmp + output1.bmp still exists one image with the caret.
System information:
Windows 10 1909
Python 3.7.9
After my test, I can use SendMessageW to send WM_KILLFOCUS to make the caret disappear.
This is the code that worked for me:
import time
import win32gui
import win32ui
import win32con
import pyautogui
from ctypes import windll
def getWindowScreenshot(hwnd, output_file):
r = win32gui.GetWindowRect(hwnd)
width = r[2] - r[0]
height = r[3] - r[1]
wDC = win32gui.GetWindowDC(hwnd)
dcObj = win32ui.CreateDCFromHandle(wDC)
cDC = dcObj.CreateCompatibleDC()
dataBitMap = win32ui.CreateBitmap()
dataBitMap.CreateCompatibleBitmap(dcObj, width, height)
cDC.SelectObject(dataBitMap)
cDC.BitBlt((0, 0), (width, height), dcObj, (0, 0), win32con.SRCCOPY)
# save the bitmap to a file
dataBitMap.SaveBitmapFile(cDC, output_file)
dcObj.DeleteDC()
cDC.DeleteDC()
win32gui.ReleaseDC(hwnd, wDC)
win32gui.DeleteObject(dataBitMap.GetHandle())
if __name__ == '__main__':
window_title = "Untitled - Notepad"
winHandle = win32gui.FindWindow(None, window_title)
if winHandle == 0:
raise RuntimeError(f"Window '{window_title}' is not opening!")
# Bring the window to foreground in case it is overlap by other windows
win32gui.SetForegroundWindow(winHandle)
# Do something with Notepad
windll.user32.SendMessageW(winHandle, win32con.WM_KILLFOCUS, None, None)
# Capture the window
getWindowScreenshot(winHandle, "output.bmp")
time.sleep(0.5)
getWindowScreenshot(winHandle, "output1.bmp")
Related
I am creating a multi touch accuracy checking device that indicates where the user should touch the screen. It overlays a semi transparent image over an exe called using subprocess with wx as shown here.
I want to output to terminal to prompt user for each data collection point but I can't exit MainLoop() to prompt the user, collect data, and repeat. Press and unpress of tab records one instance of touch in the exe.
Thank you
def scale_bitmap(bitmap, width, height):
image = wx.ImageFromBitmap(bitmap)
image = image.Scale(width, height, wx.IMAGE_QUALITY_HIGH)
result = wx.Bitmap(image)
return result
for x in range(1, 7):
app = wx.App()
trans = 100
frame1 = wx.Frame(None, -1, "KEA", style=wx.CLIP_CHILDREN | wx.STAY_ON_TOP)
# create the class instance
frame1.ShowFullScreen(True)
image_file = "6dataPoints.jpg"
bmp1 = wx.Image(image_file, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
bmp1 = scale_bitmap(bmp1, GetSystemMetrics(1) * 1.5, GetSystemMetrics(1))
bitmap1 = wx.StaticBitmap(frame1, -1, bmp1, (-100, 0))
hwnd = frame1.GetHandle()
extendedStyleSettings = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE)
win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE,
extendedStyleSettings | win32con.WS_EX_LAYERED | win32con.WS_EX_TRANSPARENT)
win32gui.SetLayeredWindowAttributes(hwnd, 0, 255, win32con.LWA_ALPHA)
frame1.SetTransparent(trans)
print("Place fingers over data collection point %d" % (x))
pyautogui.keyDown("tab")
pyautogui.keyUp("tab")
app.MainLoop()
Why do you want to use the terminal for output and prompts instead of doing it in the GUI? It would make sense to just show some (modal) dialog in the GUI instead.
If you really want to use both (blocking) terminal IO and GUI from the same program, the simplest way is to do it from two different threads, with one thread reserved for the GUI stuff only.
I think the same error as this thread; Python win32ui.error: CreateCompatibleDC failed
It's been a few months since I took a look at this code because I couldn't get past this error.
I'm using source_needle_images as the artefacts I'm trying to find, and haystack as the image I'm trying to find them in. My needles are currently two jpg's however, in future, this may be dozens of jpg's and my haystack is an application window named wincap
The eventual intention is to "do something" if anything whatsoever from source_needle_images is found within the application window.
This code is from my main.py, and I'm using two functions search and windowcapture
windowcapture captures the application window, search performs the OCR.
from windowcapture import WindowCapture
from search import Search
# the window to capture
wincap = WindowCapture('P')
# load needle images and start the matching process
source_needle_images = glob.glob(r'C:\\\\\\\*.jpg')
search = Search(source_needle_images)
loop_time = time()
while(True):
# get an updated image of the background
haystack_img = wincap.get_screenshot()
# display the processed image
points = search.find(haystack_img, 0.85, 'rectangles')
This is the section of code causing issue;
def get_screenshot(self):
# get the window image data
wDC = win32gui.GetWindowDC(self.hwnd)
dcObj = win32ui.CreateDCFromHandle(wDC)
cDC = dcObj.CreateCompatibleDC()
dataBitMap = win32ui.CreateBitmap()
dataBitMap.CreateCompatibleBitmap(dcObj, self.w, self.h)
cDC.SelectObject(dataBitMap)
cDC.BitBlt((0, 0), (self.w, self.h), dcObj, (self.cropped_x, self.cropped_y), win32con.SRCCOPY)
# convert the raw data into a format opencv can read
#dataBitMap.SaveBitmapFile(cDC, 'debug.bmp')
signedIntsArray = dataBitMap.GetBitmapBits(True)
img = np.fromstring(signedIntsArray, dtype='uint8')
img.shape = (self.h, self.w, 4)
# free resources
dcObj.DeleteDC()
cDC.DeleteDC()
win32gui.ReleaseDC(self.hwnd, wDC)
win32gui.DeleteObject(dataBitMap.GetHandle())
and this is the error in question;
Traceback (most recent call last):
File "c:\\\\\\main.py", line 23, in <module>
haystack_img = wincap.get_screenshot()
File "c:\\\\\\windowcapture.py", line 52, in get_screenshot
dataBitMap.CreateCompatibleBitmap(dcObj, self.w, self.h)
win32ui.error: CreateCompatibleDC failed
Updated with requested code:
# constructor
def __init__(self, window_name=None):
# find the handle for the window we want to capture.
# if no window name is given, capture the entire screen
if window_name is None:
self.hwnd = win32gui.GetDesktopWindow()
else:
self.hwnd = win32gui.FindWindow(None, window_name)
if not self.hwnd:
raise Exception('Window not found: {}'.format(window_name))
So I have this code:
import win32gui
import win32ui
from ctypes import windll
from PIL import Image
hwnd = win32gui.FindWindow(None, "#chat - Discord")
# Change the line below depending on whether you want the whole window
# or just the client area.
left, top, right, bot = win32gui.GetClientRect(hwnd)
#left, top, right, bot = win32gui.GetWindowRect(hwnd)
w = right - left
h = bot - top
hwndDC = win32gui.GetWindowDC(hwnd)
mfcDC = win32ui.CreateDCFromHandle(hwndDC)
saveDC = mfcDC.CreateCompatibleDC()
saveBitMap = win32ui.CreateBitmap()
saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
saveDC.SelectObject(saveBitMap)
# Change the line below depending on whether you want the whole window
# or just the client area.
result = windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 1)
#result = windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 0)
bmpinfo = saveBitMap.GetInfo()
bmpstr = saveBitMap.GetBitmapBits(True)
im = Image.frombuffer(
'RGB',
(bmpinfo['bmWidth'], bmpinfo['bmHeight']),
bmpstr, 'raw', 'BGRX', 0, 1)
if result:
#PrintWindow Succeeded
im.show()
win32gui.DeleteObject(saveBitMap.GetHandle())
saveDC.DeleteDC()
mfcDC.DeleteDC()
win32gui.ReleaseDC(hwnd, hwndDC)
The problem with it is that the screenshot it takes is all black. It does this with all windows, not just Discord. What is wrong with it, and what do I need to do to fix it?
Also as a side question - when I take screenshots the command prompt opens and then close real quick, is there also a way to stop that from happening?
The problem with it is that the screenshot it takes is all black.
I understand that you want to take a screenshot?
from PIL.ImageGrab import grab
image = grab()
image.save('Screenshot.png')
If you have the coordinates of the window, try this instead:
from PIL.ImageGrab import grab
box = (0,0,200,200)
image = grab(box)
image.save('Screenshot.png')
PIL has been discontinued though, so you may be able to use this code only for the next immediate future. Also, the command prompt does not appear with this code.
I have been having a problem with win32gui while taking screenshot of certain application window.
My script (code below) take a screenshot of a window to analyze it. For certain windows it works perfectly, but I have just encountered one application that, while I have my script running the window that it takes screenshot from make the window flickers. (I keep seeing white flash on the whole window)
Anyone ever experienced it and had a solution for it?
def getImage(self,hwnd = None):
if hwnd == None:
hwnd = self.hwnd
self.whereIsWindow(hwnd)
left, top, right, bot = win32gui.GetWindowRect(hwnd)
w = right - left
h = bot - top
self.width = w
self.height = h
hwndDC = win32gui.GetWindowDC(hwnd)
mfcDC = win32ui.CreateDCFromHandle(hwndDC)
saveDC = mfcDC.CreateCompatibleDC()
saveBitMap = win32ui.CreateBitmap()
saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
saveDC.SelectObject(saveBitMap)
result = windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 0)
bmpinfo = saveBitMap.GetInfo()
bmpstr = saveBitMap.GetBitmapBits(True)
im = Image.frombuffer(
'RGB',
(bmpinfo['bmWidth'], bmpinfo['bmHeight']),
bmpstr, 'raw', 'BGRX', 0, 1)
win32gui.DeleteObject(saveBitMap.GetHandle())
saveDC.DeleteDC()
mfcDC.DeleteDC()
win32gui.ReleaseDC(hwnd, hwndDC)
I know the culprit are the last 5 lines, if i comment them out it stops flickering but it takes all the memory which is not an option.
I ended up putting more of a delay in my loop and there was no flickering at all.
I am currently controlling a game with python by sending mouse and keystroke commands. What I am looking to do is have a transparent Tkinter window lay overtop of the game to provide some information such as mouse location and pixel color.
I am familiar with changing the window's alpha attribute to make it transparent but have no idea how to always keep that window in front and have mouse clicks pass through it.
My current method of controlling the game involves taking screenshots in certain locations and analyzing the color content. I will also need some way to do this without the Tkinter window interfering.
Pyscreenshot is used for screenshots
win32api is used for clicking
Thank you,
Alec
you can use the SetWindowLong function of win32gui module. If you want a transparent click through window you have to apply GWL_EXSTYLE's ony our window. Therefore you need the windowhandle of your Window.
hwnd = win32gui.FindWindow(None, "Your window title") # Getting window handle
# hwnd = root.winfo_id() getting hwnd with Tkinter windows
# hwnd = root.GetHandle() getting hwnd with wx windows
lExStyle = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE)
lExStyle |= win32con.WS_EX_TRANSPARENT | win32con.WS_EX_LAYERED
win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE , lExStyle )
If you want to change the transparency of your window via winapi use SetLayeredWindowAttributes.
EDIT: Examplecode for an overlay always-on-top transparent window, which pass through clicks. It gets the current desktopimage and creates a transparent overlay, so you can enjoy your desktop background image.
from win32api import GetSystemMetrics
import win32con
import win32gui
import wx
def scale_bitmap(bitmap, width, height):
image = wx.ImageFromBitmap(bitmap)
image = image.Scale(width, height, wx.IMAGE_QUALITY_HIGH)
result = wx.BitmapFromImage(image)
return result
app = wx.App()
trans = 50
# create a window/frame, no parent, -1 is default ID
# change the size of the frame to fit the backgound images
frame1 = wx.Frame(None, -1, "KEA", style=wx.CLIP_CHILDREN | wx.STAY_ON_TOP)
# create the class instance
frame1.ShowFullScreen(True)
image_file = win32gui.SystemParametersInfo(win32con.SPI_GETDESKWALLPAPER,0,0)
bmp1 = wx.Image(image_file, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
bmp1 = scale_bitmap(bmp1,GetSystemMetrics(1)*1.5,GetSystemMetrics(1))
bitmap1 = wx.StaticBitmap(frame1, -1, bmp1, (-100, 0))
hwnd = frame1.GetHandle()
extendedStyleSettings = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE)
win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, extendedStyleSettings | win32con.WS_EX_LAYERED | win32con.WS_EX_TRANSPARENT)
win32gui.SetLayeredWindowAttributes(hwnd, 0, 255, win32con.LWA_ALPHA)
frame1.SetTransparent(trans)
def onKeyDown(e):
global trans
key = e.GetKeyCode()
if key==wx.WXK_UP:
print trans
trans+=10
if trans >255:
trans = 255
elif key==wx.WXK_DOWN:
print trans
trans-=10
if trans < 0:
trans = 0
try:
win32gui.SetLayeredWindowAttributes(hwnd, 0, trans, win32con.LWA_ALPHA)
except:
pass
frame1.Bind(wx.EVT_KEY_DOWN, onKeyDown)
app.MainLoop()
You can dynamically change the transparency with the arrow keys Up/Down.
Notice, the windowframe is created with 'wx', but should work with tkinter also.
Feel free to use the code as you like.