如何截取可滚动窗口的完整内容?现有代码无法实现滚动截图需求
Solution to Capture Full Systeminfo Output via Scrolling Screenshots
To capture the complete output of systeminfo by scrolling and stitching screenshots, here's a revised implementation that addresses the need for vertical scrolling and full content capture:
Key Improvements
- Activates and maximizes the Command Prompt window to optimize screen real estate
- Scrolls to the top of the output before starting captures
- Takes incremental screenshots while scrolling down until no new content is visible
- Stitches screenshots together (removing overlap) to create a single full-length image
Required Packages
Install the necessary libraries first:
pip install pyautogui pygetwindow pillow
Full Code
import pyautogui import subprocess import time import pygetwindow as gw from PIL import Image # Step 1: Open Command Prompt and prepare the window subprocess.Popen('start cmd', shell=True) time.sleep(2) # Locate, activate, and maximize the Command Prompt window cmd_windows = gw.getWindowsWithTitle('Command Prompt') if cmd_windows: cmd_window = cmd_windows[0] cmd_window.activate() cmd_window.maximize() time.sleep(1) # Step 2: Execute systeminfo command pyautogui.write('systeminfo') pyautogui.press('enter') time.sleep(10) # Adjust based on system speed to ensure command completes # Step 3: Scroll to the top of the output pyautogui.press('home') time.sleep(0.5) # Step 4: Capture incremental screenshots while scrolling screenshots = [] scroll_step = 30 # Number of down-arrow presses per scroll (adjust as needed) overlap_pixels = 200 # Overlap between screenshots to ensure continuity (adjust as needed) # Capture initial screenshot screenshots.append(pyautogui.screenshot()) time.sleep(0.5) last_screenshot = None while True: # Scroll down by the defined step for _ in range(scroll_step): pyautogui.press('down') time.sleep(0.5) # Capture new screenshot current_screenshot = pyautogui.screenshot() # Check if we've reached the end (no new content) if last_screenshot and current_screenshot.tobytes() == last_screenshot.tobytes(): break screenshots.append(current_screenshot) last_screenshot = current_screenshot # Step 5: Stitch screenshots into one full image def stitch_screenshots(images, overlap): if not images: return None # Calculate total height after removing overlaps total_height = images[0].height + sum(img.height - overlap for img in images[1:]) stitched_img = Image.new('RGB', (images[0].width, total_height)) y_offset = 0 stitched_img.paste(images[0], (0, y_offset)) y_offset += images[0].height for img in images[1:]: # Crop overlapping top portion cropped_img = img.crop((0, overlap, img.width, img.height)) stitched_img.paste(cropped_img, (0, y_offset)) y_offset += cropped_img.height return stitched_img # Generate and save the full screenshot full_screenshot = stitch_screenshots(screenshots, overlap_pixels) if full_screenshot: full_screenshot.save('systeminfo_full_screenshot.png') print("Full screenshot saved as 'systeminfo_full_screenshot.png'") else: print("Failed to capture any screenshots.")
Notes
- Adjust
scroll_stepbased on how much you want to scroll each time (more steps = fewer screenshots) - Tweak
overlap_pixelsto ensure no content is missing between screenshots (depends on your screen resolution and cmd font size) - The
time.sleepvalues may need adjustment based on your system's responsiveness
内容的提问来源于stack exchange,提问作者cybersecCoder
相关产品推荐
相关产品推荐

