Python实现截屏的函数
本文实例讲述了Python实现截屏的函数。分享给大家供大家参考。具体如下:
1.可指定保存目录.
2.截屏图片名字以时间为文件名
3.截屏图片存为JPG格式图片,比BMP小多的,一个1024*800的截屏BMP有3M多,一个1024*800的JPG只有300K左右.
就可做一个简单的监控了,每10秒截一屏,放到一个指定隐藏的文件夹里,基本掌握机子的使用了,适合监控自家小孩的使用情况
# -*- coding: cp936 -*- import time,Image import os, win32gui, win32ui, win32con, win32api def window_capture(dpath): ''''' 截屏函数,调用方法window_capture('d:\\') ,参数为指定保存的目录 返回图片文件名,文件名格式:日期.jpg 如: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程序设计有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Regarding the problem of removing the Python interpreter that comes with Linux systems, many Linux distributions will preinstall the Python interpreter when installed, and it does not use the package manager...

Pylance type detection problem solution when using custom decorator In Python programming, decorator is a powerful tool that can be used to add rows...

About Pythonasyncio...

Using python in Linux terminal...

Loading pickle file in Python 3.6 environment error: ModuleNotFoundError:Nomodulenamed...

Compatibility issues between Python asynchronous libraries In Python, asynchronous programming has become the process of high concurrency and I/O...

Error loading Pickle file in Python 3.6 environment: ModuleNotFoundError:Nomodulenamed...

The problem and solution of the child process continuing to run when using signals to kill the parent process. In Python programming, after killing the parent process through signals, the child process still...
