脚本怎样截取屏幕截图

wen 实用脚本 28

本文目录导读:

脚本怎样截取屏幕截图

  1. Python 脚本截图
  2. JavaScript 浏览器截图
  3. Shell 脚本截图
  4. Windows PowerShell
  5. 自动化测试截图
  6. 实用功能增强
  7. 批量截图工具

Python 脚本截图

使用 pyautogui(跨平台)

import pyautogui
# 截取全屏
screenshot = pyautogui.screenshot()
screenshot.save('screenshot.png')
# 截取指定区域 (left, top, width, height)
region = pyautogui.screenshot(region=(0, 0, 500, 400))
region.save('region.png')

使用 Pillow (PIL)

from PIL import ImageGrab
# 截取全屏
im = ImageGrab.grab()
im.save('screenshot.png')
# 截取指定区域
im = ImageGrab.grab(bbox=(100, 100, 500, 500))
im.save('region.png')

使用 mss(高性能)

import mss
import mss.tools
with mss.mss() as sct:
    # 全屏截图
    sct.shot(output='screenshot.png')
    # 指定显示器
    monitor = sct.monitors[1]  # 第一个显示器
    sct.shot(mon=monitor, output='monitor.png')

JavaScript 浏览器截图

截取整个页面

// 需要 html2canvas 库
async function captureScreenshot() {
    const canvas = await html2canvas(document.body);
    const image = canvas.toDataURL('image/png');
    // 创建下载链接
    const link = document.createElement('a');
    link.download = 'screenshot.png';
    link.href = image;
    link.click();
}

截取特定元素

async function captureElement(elementId) {
    const element = document.getElementById(elementId);
    const canvas = await html2canvas(element);
    return canvas.toDataURL('image/png');
}

Shell 脚本截图

Linux (使用 scrot)

#!/bin/bash
# 截取全屏
scrot 'screenshot_%Y%m%d_%H%M%S.png'
# 延迟5秒截图
scrot -d 5 'delayed_screenshot.png'
# 截取窗口
scrot -b 'window_%Y%m%d.png'

macOS

#!/bin/bash
# 截取全屏
screencapture screen.png
# 截取选择区域
screencapture -i region.png
# 截取窗口
screencapture -w window.png

Windows PowerShell

# 使用 .NET 类
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$bounds = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds
$bitmap = New-Object System.Drawing.Bitmap $bounds.Width, $bounds.Height
$graphics = [System.Drawing.Graphics]::FromImage($bitmap)
$graphics.CopyFromScreen($bounds.Location, [System.Drawing.Point]::Empty, $bounds.Size)
$bitmap.Save("screenshot.png", [System.Drawing.Imaging.ImageFormat]::Png)

自动化测试截图

Selenium (Python)

from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://example.com')
# 截取整个页面
driver.save_screenshot('full_page.png')
# 截取元素
element = driver.find_element_by_id('some-element')
element.screenshot('element.png')
driver.quit()

实用功能增强

定时截图

import time
import pyautogui
def timed_screenshots(interval_seconds, count):
    for i in range(count):
        pyautogui.screenshot(f'screenshot_{i}.png')
        time.sleep(interval_seconds)
# 每5秒截一次,共10次
timed_screenshots(5, 10)

添加时间戳

from datetime import datetime
import pyautogui
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
pyautogui.screenshot(f'screenshot_{timestamp}.png')

上传到云存储

import pyautogui
import requests
# 截图并上传
screenshot = pyautogui.screenshot()
screenshot.save('temp.png')
with open('temp.png', 'rb') as f:
    files = {'file': f}
    response = requests.post('https://your-upload-url', files=files)

批量截图工具

import os
import pyautogui
from selenium import webdriver
def batch_screenshot(urls, output_dir):
    os.makedirs(output_dir, exist_ok=True)
    driver = webdriver.Chrome()
    for i, url in enumerate(urls):
        driver.get(url)
        driver.save_screenshot(f'{output_dir}/page_{i}.png')
    driver.quit()
# 使用示例
urls = ['https://example1.com', 'https://example2.com']
batch_screenshot(urls, './screenshots')

选择哪种方法取决于你的具体需求:

  • Python: 最灵活,适合自动化任务
  • JavaScript: 用于 Web 应用
  • Shell: 适合简单快速的操作
  • PowerShell: Windows 环境

抱歉,评论功能暂时关闭!