0

I am using a very simple code in python 2.7.11 under windows 7.

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. 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.

Ren
  • 2,852
  • 2
  • 23
  • 45
Richard
  • 703
  • 3
  • 11
  • 33

2 Answers2

1

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.

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).

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:

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())
Community
  • 1
  • 1
Torxed
  • 22,866
  • 14
  • 82
  • 131
0

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. 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.

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