简体   繁体   English

屏幕截图未在python中显示所有窗口屏幕截图imagegrab

[英]screenshot not showing all windows in python screenshot imagegrab

I am using a very simple code in python 2.7.11 under windows 7. 我在Windows 7下的python 2.7.11中使用了非常简单的代码。

def takeScreenshot(filename,directories,allConfigs):
    im = pyautogui.screenshot()
    im.save(os.path.join(directories['picsdir'],filename))

When I use this function, it takes a screenshot of the whole screen except the application that I really want which is samsung sidesync. 当我使用此功能时,它将截取整个屏幕的屏幕截图,但我真正想要的应用程序是三星sidesync。 When using this code, it shows me the whole screen but it is as if the samsung sidesync window has disappeared in the screenshot. 使用此代码时,它将向我显示整个屏幕,但好像三星侧向同步窗口已在屏幕截图中消失了。

But if I do a print screen from the keyboard button and save to a file, it is there and I can see it. 但是,如果我通过键盘按钮执行打印屏幕并将其保存到文件,则可以在其中看到它。

I don't understand why the behavior should be different. 我不明白为什么行为应该有所不同。

As to why this happens, the most likely reason is that Samsung sidesync uses DirectX/OpenGL/LayeredGraphics to render the screen image of your phone. 至于发生这种情况的原因,最可能的原因是Samsung sidesync使用DirectX / OpenGL / LayeredGraphics渲染手机的屏幕图像。

This is why most users pace applications won't be able to pick up what's shown to you on your monitor because that graphical rendering instance is outside of your application scope (try rendering GL graphics and have a thread within your own code update sad GL instance and you'll get my point here). 这就是为什么大多数用户无法调整应用程序进度的原因,因为该图形渲染实例不在您的应用程序范围内(请尝试渲染GL图形并在您自己的代码中包含一个线程来更新悲伤的GL实例)然后您会明白我的意思)。

Now your library is just an abstraction of the following code: 现在,您的库只是以下代码的抽象:

from PIL import ImageGrab
screenshot = ImageGrab.grab()
screenshot.save('wham.png')

Now the following code hooks into Windows system libraries which should live in system space and thus have more access to grab your layered windows: 现在, 以下代码挂接到Windows系统库中,该系统库应存在于系统空间中,因此可以更轻松地获取分层窗口:

import win32gui
import win32ui 
hwnd = win32gui.FindWindow(None, windowname)
wDC = win32gui.GetWindowDC(hwnd)
dcObj=win32ui.CreateDCFromHandle(wDC)
cDC=dcObj.CreateCompatibleDC()
dataBitMap = win32ui.CreateBitmap()
dataBitMap.CreateCompatibleBitmap(dcObj, w, h)
cDC.SelectObject(dataBitMap)
cDC.BitBlt((0,0),(w, h) , dcObj, (0,0), win32con.SRCCOPY)
dataBitMap.SaveBitmapFile(cDC, bmpfilenamename)
# Free Resources
dcObj.DeleteDC()
cDC.DeleteDC()
win32gui.ReleaseDC(hwnd, wDC)
win32gui.DeleteObject(dataBitMap.GetHandle())

the accepted answer does not work (or possibly does not work anymore). 接受的答案不起作用(或可能不再起作用)。 using windows 7 and python 2.7 3.5 and 3.6 the accepted answer does not show the sidesync GUI in the captured images. 使用Windows 7和python 2.7 3.5和3.6,接受的答案不会在捕获的图像中显示sidesync GUI。 Because the GUI does show up in normal screen shots, you can simply trigger a screen shot and then read the image data from the clipboard. 因为GUI确实以正常的屏幕截图显示,所以您只需触发屏幕截图,然后从剪贴板读取图像数据即可。

from ahk import AHK
from PIL import ImageGrab
ahk=AHK()
ahk.send('!{PrintScreen}')
img = ImageGrab.grabclipboard()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM