python调用win32接口进行截图的示例
作者:enumx 时间:2021-07-22 07:19:45
python的版本及依赖的库的安装
#版本python 3.7.1
pip install pywin32==224
pip install numpy==1.15.3
pip install opencv-python==3.4.2.16
pip install opencv-contrib-python==3.4.2.16
pip install Pillow-PIL==0.1.dev0
对后台窗口截图
#对后台窗口截图
import win32gui, win32ui, win32con
from ctypes import windll
from PIL import Image
import cv2
import numpy
#获取后台窗口的句柄,注意后台窗口不能最小化
hWnd = win32gui.FindWindow("NotePad",None) #窗口的类名可以用Visual Studio的SPY++工具获取
#获取句柄窗口的大小信息
left, top, right, bot = win32gui.GetWindowRect(hWnd)
width = right - left
height = bot - top
#返回句柄窗口的设备环境,覆盖整个窗口,包括非客户区,标题栏,菜单,边框
hWndDC = win32gui.GetWindowDC(hWnd)
#创建设备描述表
mfcDC = win32ui.CreateDCFromHandle(hWndDC)
#创建内存设备描述表
saveDC = mfcDC.CreateCompatibleDC()
#创建位图对象准备保存图片
saveBitMap = win32ui.CreateBitmap()
#为bitmap开辟存储空间
saveBitMap.CreateCompatibleBitmap(mfcDC,width,height)
#将截图保存到saveBitMap中
saveDC.SelectObject(saveBitMap)
#保存bitmap到内存设备描述表
saveDC.BitBlt((0,0), (width,height), mfcDC, (0, 0), win32con.SRCCOPY)
#如果要截图到打印设备:
###最后一个int参数:0-保存整个窗口,1-只保存客户区。如果PrintWindow成功函数返回值为1
#result = windll.user32.PrintWindow(hWnd,saveDC.GetSafeHdc(),0)
#print(result) #PrintWindow成功则输出1
#保存图像
##方法一:windows api保存
###保存bitmap到文件
saveBitMap.SaveBitmapFile(saveDC,"img_Winapi.bmp")
##方法二(第一部分):PIL保存
###获取位图信息
bmpinfo = saveBitMap.GetInfo()
bmpstr = saveBitMap.GetBitmapBits(True)
###生成图像
im_PIL = Image.frombuffer('RGB',(bmpinfo['bmWidth'],bmpinfo['bmHeight']),bmpstr,'raw','BGRX',0,1)
##方法二(后续转第二部分)
##方法三(第一部分):opencv+numpy保存
###获取位图信息
signedIntsArray = saveBitMap.GetBitmapBits(True)
##方法三(后续转第二部分)
#内存释放
win32gui.DeleteObject(saveBitMap.GetHandle())
saveDC.DeleteDC()
mfcDC.DeleteDC()
win32gui.ReleaseDC(hWnd,hWndDC)
##方法二(第二部分):PIL保存
###PrintWindow成功,保存到文件,显示到屏幕
im_PIL.save("im_PIL.png") #保存
im_PIL.show() #显示
##方法三(第二部分):opencv+numpy保存
###PrintWindow成功,保存到文件,显示到屏幕
im_opencv = numpy.frombuffer(signedIntsArray, dtype = 'uint8')
im_opencv.shape = (height, width, 4)
cv2.cvtColor(im_opencv, cv2.COLOR_BGRA2RGB)
cv2.imwrite("im_opencv.jpg",im_opencv,[int(cv2.IMWRITE_JPEG_QUALITY), 100]) #保存
cv2.namedWindow('im_opencv') #命名窗口
cv2.imshow("im_opencv",im_opencv) #显示
cv2.waitKey(0)
cv2.destroyAllWindows()
对桌面截图
截图整个桌面
import win32gui
import win32ui
import win32con
import win32api
# 获取桌面
hdesktop = win32gui.GetDesktopWindow()
# 分辨率适应
width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)
# 创建设备描述表
desktop_dc = win32gui.GetWindowDC(hdesktop)
img_dc = win32ui.CreateDCFromHandle(desktop_dc)
# 创建一个内存设备描述表
mem_dc = img_dc.CreateCompatibleDC()
# 创建位图对象
screenshot = win32ui.CreateBitmap()
screenshot.CreateCompatibleBitmap(img_dc, width, height)
mem_dc.SelectObject(screenshot)
# 截图至内存设备描述表
mem_dc.BitBlt((0, 0), (width, height), img_dc, (0, 0), win32con.SRCCOPY)
# 将截图保存到文件中
screenshot.SaveBitmapFile(mem_dc, 'screenshot.bmp')
# 内存释放
mem_dc.DeleteDC()
win32gui.DeleteObject(screenshot.GetHandle())
来源:https://www.cnblogs.com/enumx/p/12337132.html
标签:python,win32,接口,截图
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python 详解如何使用GPU大幅提高效率
2023-08-24 19:45:46
![](https://img.aspxhome.com/file/2023/0/62550_0s.png)
SQL Server 2005数据库批量更新解决办法
2009-04-11 16:12:00
详细讲解删除SQL Server日志的具体方法
2008-12-09 14:32:00
HTML与CSS在Flash中的应用
2008-01-23 19:13:00
js 仿Photoshop鼠标滚轮控制输入框取值(修正兼容Chrome)
2010-02-05 12:27:00
CSS鼠标悬停tip效果
2007-08-26 17:32:00
![](https://img.aspxhome.com/file/UploadPic/20078/26/200782618155189s.gif)
SQL Server数据库入门学习总结
2012-08-21 11:01:33
mysql慢查询的分析方法
2010-08-03 14:51:00
Go的固定时长定时器和周期性时长定时器
2023-06-27 12:44:23
解决PHP 7编译安装错误:cannot stat ‘phar.phar’: No such file or directory
2023-09-05 06:07:44
![](https://img.aspxhome.com/file/2023/2/55632_0s.jpg)
WAP设计基础
2011-01-06 12:13:00
![](https://img.aspxhome.com/file/UploadPic/20111/6/1-56s.png)
你是真正的用户体验设计者吗? Ⅴ
2008-04-19 18:32:00
![](https://img.aspxhome.com/file/UploadPic/20084/19/2008419215352323s.jpg)
搜索结果页(SERP):前言
2009-07-22 20:56:00
![](https://img.aspxhome.com/file/UploadPic/20097/22/01-73s.jpg)
关于window.opener的用法
2008-02-23 11:03:00
Bootstrap实现圆角、圆形头像和响应式图片
2023-08-12 07:16:59
![](https://img.aspxhome.com/file/2023/2/56052_0s.jpg)
PHP中soap的用法实例
2023-11-14 09:40:24
laravel的用户修改密码与绑定邮箱的详细操作
2023-06-17 09:55:06
![](https://img.aspxhome.com/file/2023/5/55535_0s.jpg)
PHP实现多文件上传的方法
2023-11-23 03:42:04
PHP count_chars()函数讲解
2023-06-05 09:17:25
![](https://img.aspxhome.com/file/2023/2/55392_0s.png)
SQL Server如何保证可空字段中非空值唯一
2011-02-24 16:44:00