Python's function of intercepting screen
This article describes the Python implementation of the screenshot function. Share to everyone for your reference. Specifically as follows:
1. You can specify the save directory.
2. Screenshot picture name with time as filename
3. Screenshot image saved as JPG format picture, more than BMP, a 1024*800 screen bmp has more than 3M, a 1024*800 jpg only 300K or so.
Can do a simple monitoring, every 10 seconds to cut a screen, placed in a designated hidden folder, the basic grasp of the use of the machine, suitable for monitoring the use of their children
#-*-coding:cp936-*-
import time,image
import OS, Win32gui, Win32ui, Win32con, Win32API
def window _capture (Dpath):
'''''''
Screenshot function, call method Window_capture (' d:\\ '), parameter is the specified saved directory
return picture filename, file name format: date. jpg such as: 2009328224853.jpg
' ' hwnd = 0 hwnddc = win32gui. GETWINDOWDC (HWND) mfcdc=win32ui. Createdcfromhandle (HWNDDC) savedc=mfcdc.createcompatibledc () savebitmap = win32ui. CreateBitmap () moniterdev=win32api. Enumdisplaymonitors (none,none) w = MoniterDev[0][2][2] h = MoniterDev[0][2][3] #print w,h #图片大小 savebitmap.createcompatiblebitmap (mfcdc, w, h) savedc.selectobject (Savebitmap) savedc.bitblt ((0,0), (w, h) , mfcdc, (0,0), win32con. srccopy) cc=time.gmtime () bmpname=str (Cc[0]) +str (cc[1]) +str (cc[2]) +str (cc[3]+8) +str (cc[4)) +str (cc[5]) + '. bmp ' Savebitmap.savebitmapfile (savedc, bmpname) image.open (bmpname). Save (bmpname[ : -4]+ ". jpg") os.remove (bmpname) jpgname=bmpname[:-4] + '. jpg ' djpgname=dpath+jpgname copy_command = "move %s %s" % (jpgname, djpgname) os.popen (copy_ command) return bmpname[:-4]+ '. jpg ' #调用截屏函数 window_capture (' d:\\ ')
Python implementation Screen capture
Because I want to add a screenshot in one of my programs, today I look at how to use Python screenshot, the realization of the function is quite simple, directly on the code
From PIL import Imagegrab
im = Imagegrab.grab ()
Im.save (addr, ' jpeg ')
A few lines of code to achieve the function I want, PIL (Python Image library) is a Python graphics library, need to download the installation itself, Im = Imagegrab.grab () This line of code to achieve screenshots function, can take parameters, Specifies that you want to intercept the coordinates of the picture, without the parameters of the default Full-screen screenshot, Im.save (addr, ' jpeg ') is to save the captured picture, the first parameter is the save path, the second parameter is the picture format
Python calls QQ screenshot tool
Yesterday to listen to colleagues said that you want to open QQ to use the screenshot provided by the function, although only one said, I am very interested, so from yesterday afternoon to just use Python to achieve a bit. Only to achieve the screen-cutting function of the call, and not very practical, when it is playing.
To the QQ installation directory to find, there is a Camera.dll, looks and screenshots related, search, found that the net mentioned should be CameraDll.dll, may be later revised, with tools to view the Camera.dll, as shown in the picture:
Looks like there is nothing to do with screenshots related to the function, and the Internet refers to the call of the Camerasubarea did not see, so down an old version of the DLL, that is, CameraDll.dll, open look:
This time you see the correlation function, and then you can call the DLL.
The Python code is as follows:
Import ctypes Import os #截屏方法 def capture (): try: dll = ctypes.cdll.loadlibrary (' CameraDll.dll ') except exception: print ("dll load error!") return else: try: dll. Camerasubarea (0) except exception: print ("sth wrong in capture!") return #获取计算机名 def getname ():
pcname = ctypes.c_char_p ('. Encode (' Utf-8 ')) pcsize = 16 Pcname = ctypes.cast (pcname, ctypes.c_char_p) try: ctypes.windll.kernel32.getcomputernamea (Pcname, ctypes.byref (Ctypes.c_int) ( pcsize)) except exception: print ("
sth wrong in getname! ")
print (PcName.value.decode (' Utf-8 ')) Def main (): getname () #调用截屏 capture () os.system ("pause") if __name__ == "__main__": main ()
After loading the DLL, call Camerasubarea, although it is not clear what parameters should be passed in, but does not affect the use.
Another method is to practice loading the system DLL to get the computer name.
Use the effect as shown in the figure: