Python如何截图保存的三种方法(小结)
作者:忆昔如风 时间:2023-08-24 22:17:19
本文介绍python如何进行截图保存的几种方法,在测试过程中,是有必要截图,特别是遇到错误的时候进行截图。结合Python其它模块如time ,os.path,基本能满足截图保存文件的功能需求
第一种 selenium for python
get_screenshot_as_file()
相关代码如下:
# coding=utf-8
import time
from selenium import webdriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(6)
driver.get("https://www.baidu.com")
time.sleep(1)
driver.get_screenshot_as_file("C:\\baidu.png")
driver.quit()
第二种 使用PIL库
代码如下:
import time
from PIL import ImageGrab
if __name__=="__main__":
pic = ImageGrab.grab()
pic.save("picture.jpg")
第三种
代码如下:
#coding=utf-8
import time
import win32gui, win32ui, win32con, win32api
def window_capture(filename):
hwnd = 0 # 窗口的编号,0号表示当前活跃窗口
# 根据窗口句柄获取窗口的设备上下文DC(Divice Context)
hwndDC = win32gui.GetWindowDC(hwnd)
# 根据窗口的DC获取mfcDC
mfcDC = win32ui.CreateDCFromHandle(hwndDC)
# mfcDC创建可兼容的DC
saveDC = mfcDC.CreateCompatibleDC()
# 创建bigmap准备保存图片
saveBitMap = win32ui.CreateBitmap()
# 获取监控器信息
MoniterDev = win32api.EnumDisplayMonitors(None, None)
w = MoniterDev[0][2][2]
h = MoniterDev[0][2][3]
# print w,h#图片大小
# 为bitmap开辟空间
saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
# 高度saveDC,将截图保存到saveBitmap中
saveDC.SelectObject(saveBitMap)
# 截取从左上角(0,0)长宽为(w,h)的图片
saveDC.BitBlt((0, 0), (w, h), mfcDC, (0, 0), win32con.SRCCOPY)
saveBitMap.SaveBitmapFile(saveDC, filename)
if __name__=="__main__":
beg = time.time()
for i in range(1):
image_name="haha%s.jpg"%i
window_capture(image_name)
end = time.time()
print(end - beg)
来源:https://blog.csdn.net/wuq757693255/article/details/90056176
标签:Python,截图保存
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
pythonfor循环中range与len区别
2023-06-22 03:17:10
![](https://img.aspxhome.com/file/2023/2/115022_0s.png)
SQL Server数据库超级管理员账号防护
2008-12-22 16:30:00
详解vscode实现远程linux服务器上Python开发
2021-07-02 05:09:32
![](https://img.aspxhome.com/file/2023/2/117522_0s.png)
Matplotlib条形图之分组条形图和堆叠条形图详解
2021-08-10 15:16:24
![](https://img.aspxhome.com/file/2023/5/124185_0s.png)
介绍Python的Urllib库的一些高级用法
2023-03-29 08:28:06
![](https://img.aspxhome.com/file/2023/5/93825_0s.png)
通过python-turtle库实现绘制图画
2023-11-23 17:39:35
![](https://img.aspxhome.com/file/2023/5/107595_0s.png)
Python 代码性能优化技巧分享
2023-10-15 05:00:56
![](https://img.aspxhome.com/file/2023/3/107203_0s.gif)
Python实现生活常识解答机器人
2021-06-22 14:34:36
![](https://img.aspxhome.com/file/2023/6/70726_0s.png)
Golang断言判断值类型的实现方法
2024-02-16 09:59:38
![](https://img.aspxhome.com/file/2023/3/107343_0s.jpg)
Perl 哈希Hash用法之入门教程
2023-11-06 22:57:17
Python中关于 *args与**args的用法及说明
2022-04-01 08:24:38
![](https://img.aspxhome.com/file/2023/0/97750_0s.png)
基于python神经卷积网络的人脸识别
2023-11-03 17:37:17
![](https://img.aspxhome.com/file/2023/6/64356_0s.png)
在IPython中执行Python程序文件的示例
2023-02-20 09:06:14
Python 中的异步 for 循环示例详解
2021-07-09 10:27:30
使用PHP获取网络文件的实现代码
2023-09-09 08:41:53
Vue之使用mockjs生成模拟数据案例详解
2024-05-29 22:24:40
![](https://img.aspxhome.com/file/2023/5/123105_0s.png)
linux下默认安装mysql数据库
2011-03-08 09:24:00
python怎么判断素数
2021-09-30 11:10:33
一篇文章弄懂Python关键字、标识符和变量
2022-11-01 17:44:42
![](https://img.aspxhome.com/file/2023/4/66294_0s.png)
使用Python-pptx 告别繁琐的幻灯片制作
2021-12-10 12:03:23
![](https://img.aspxhome.com/file/2023/4/133724_0s.png)