Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.

For a Python script running while a desktop session is available, the shortest route is PyAutoGUI: pyautogui.screenshot("screenshot.png") captures the screen and saves it. The important limit is what “background” means: running unattended does not give a script access to a display that is unavailable, nor does a screen capture necessarily reveal an application hidden behind other windows. If you need a screenshot of a web page rather than the computer’s desktop, ScreenshotNeo is a separate API option; it does not capture desktop applications.

Capture and save a desktop screenshot with PyAutoGUI

Install PyAutoGUI and its screenshot prerequisites for the operating system and release you will deploy. The PyAutoGUI documentation says screenshot support requires Pillow and lists scrot as a Linux dependency; on macOS it uses the system screencapture command. Check the current installation guidance for your distribution before automating the script.

import pyautogui

image = pyautogui.screenshot("screenshot.png")

This saves the screenshot as screenshot.png and returns the corresponding Pillow image in image, so you can continue processing it in Python if needed. The filename is relative to the process’s working directory. For a scheduled task or service, use an absolute output path so the destination is unambiguous.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Capture a rectangular region

Pass region=(left, top, width, height) to capture part of the screen. These are screen coordinates and dimensions, not the right and bottom edges.

import pyautogui

image = pyautogui.screenshot(
    "/var/tmp/screenshot.png",
    region=(0, 0, 800, 600),
)

Verify the coordinates on the actual display and confirm that the requested rectangle lies within its bounds. Multi-monitor arrangements and display scaling can make it important to test the coordinate system in the same environment where the script will run.

Choose a capture library for the job

PyAutoGUI, MSS, and Pillow’s ImageGrab all provide ways to capture pixels from a computer display, but their interfaces and platform-specific options differ. Choose based on whether you need one simple screenshot, repeated monitor capture, image processing, or a supported window-specific capture—not on an assumed universal speed ranking.

Need Starting point What to check
One full-screen image or a rectangular region PyAutoGUI Pillow and operating-system capture prerequisites; region coordinates.
Repeated captures, explicit monitor or region selection, or pixel processing MSS Display/backend access, monitor selection, and how you will convert or save the captured pixels.
A Pillow-based workflow, Windows multi-screen capture, or supported single-window capture ImageGrab Installed Pillow version and the exact operating system and API support for the arguments you plan to use.
A web page screenshot rather than a desktop or application window ScreenshotNeo It captures a requested website URL through an API; it is not a substitute for capturing arbitrary desktop pixels.

Use MSS for monitor selection or repeated captures

MSS lets you select a monitor or region, returns pixel data, and documents both PNG output and conversion to a Pillow image. For repeated captures, its usage guidance recommends reusing an MSS instance rather than opening a new one for every frame.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
from mss import MSS

with MSS() as sct:
    image = sct.grab(sct.primary_monitor).to_pil()
    image.save("screenshot.png")

Use the monitor list or primary-monitor property according to the target display; do not assume the primary display is the one you want on a multi-monitor machine. MSS also documents saving PNG data with mss.tools.to_png(...) and region capture through grab(...). See the MSS usage documentation and MSS examples for the supported monitor and region forms.

Linux display selection

On Linux, MSS uses the DISPLAY environment variable by default and accepts an explicit display, for example MSS(display=":0.0"). The process still needs permission and an accessible graphical display. Setting a variable alone does not create a desktop session or guarantee access to another user’s display.

Use Pillow ImageGrab for Pillow-centric workflows

PIL.ImageGrab.grab() captures the entire screen by default, or a bounding box when one is supplied. The documentation specifies RGBA pixels on macOS and RGB pixels elsewhere. On Windows, all_screens is documented for capturing across screens.

ImageGrab also documents a window argument for a single window on Windows (HWND) and macOS (CGWindowID); those capabilities were introduced in Pillow 11.2.1 and 12.1.0 respectively. Check the installed version and the current documentation before relying on those parameters. A window-specific API is a different requirement from capturing the whole screen, and platform behavior should be tested on the actual target system.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

On Linux, when the default X11 display does not return a snapshot, the ImageGrab documentation describes fallback use of gnome-screenshot, grim, or spectacle when installed. See the Pillow ImageGrab documentation for current argument and platform details.

Make background execution dependable

A script that runs as a daemon, scheduled job, or unattended worker can only capture a screen if the relevant graphical session is available to that process. First validate capture in the same account, environment, and display session that will run the background job; an interactive run under your own login does not establish that a service account has equivalent access.

  • Confirm display access. On Linux, inspect DISPLAY and verify that the process can access it. MSS documents this variable as its default display source. A headless machine with no accessible desktop should not be assumed to have desktop pixels to capture.
  • Use an explicit output destination. Create the directory beforehand and grant the service account write access. An absolute path avoids dependence on a scheduler’s or daemon’s working directory.
  • Decide whether files should be replaced or retained. A stable filename overwrites or reuses the same destination depending on the save path and library behavior; use a timestamped filename when each run should produce a separate artifact. MSS examples include callback handling for an existing screenshot filename.
  • Check region geometry. Verify the coordinate origin, bounds, monitor, and scaling on the target display. PyAutoGUI expresses regions as left, top, width, and height; ImageGrab accepts a bounding box; MSS accepts monitor or region forms.
  • Protect captured content. Screenshots may contain credentials, personal information, or other sensitive material. Choose a restricted output directory and a retention policy appropriate to what the display can show.

Understand timing, reliability, and cost

PyAutoGUI’s documentation gives roughly 100 milliseconds as an illustrative screenshot time for a 1920 × 1080 screen. That is an example in its documentation, not an independent comparison or a guarantee for your hardware, operating system, display backend, or capture region. The official documentation considered here does not establish a generally applicable performance winner among PyAutoGUI, MSS, and ImageGrab. If capture latency matters, time the actual operation under the same session and load as deployment.

For recurring jobs, reliability usually depends less on the Python call than on whether the intended session stays available, the process has display and file permissions, and the destination has adequate space. Add application-level logging around capture and saving so a scheduled run can distinguish display-access failures from write failures. Retention and cleanup are deployment choices; the libraries do not determine how long your files should be kept.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Troubleshoot common failures

The script works interactively but fails as a service

The service may run under a different account, have a different working directory, or lack access to the logged-in display. Run a test as the service account in its actual launch environment, inspect the display configuration, and use an absolute writable output path. If the host has no accessible graphical session, a desktop screenshot is not available merely because Python is running.

Linux capture reports a missing dependency or cannot connect to a display

PyAutoGUI’s screenshot documentation lists Pillow and scrot for Linux; confirm the prerequisites for your distribution and installed release. For MSS, check whether DISPLAY points to the intended session and whether the process is allowed to connect. MSS supports an explicit display argument, but that only selects a display; it does not grant access to it.

The image is blank, incomplete, or from the wrong monitor

Confirm that the capture ran in the expected graphical session and that the selected monitor is correct. For a region, inspect the coordinate origin and width/height or bounding-box values against the real display geometry. Test with the same display scaling and monitor arrangement used in production.

The file is missing or an older image appears

Check the process’s working directory if the filename is relative, verify that the destination directory exists, and test write access as the scheduled account. Use an absolute path and a unique timestamped name if each run should be retained rather than writing repeatedly to one filename.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A window capture argument is rejected

Check the installed Pillow version and the operating system. ImageGrab’s documented window support is version-specific: Windows support was introduced in Pillow 11.2.1 and macOS support in 12.1.0. Use a whole-screen or region capture if the installed platform/API combination does not support the window option you need.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

If your target is a web page, not a desktop screen or hidden application window, ScreenshotNeo can return a screenshot from one GET request. It accepts cookie or consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; those cleanup steps can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and responses identify the result with X-Page-Verdict and X-Billed headers. Its MCP server provides take_screenshot, get_page_info, and capture_pdf tools for AI agents and MCP clients.

The API and its options are documented at ScreenshotNeo docs. This cURL example saves a screenshot of a URL as a WebP file; replace the sample target and supply your API key.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

For a Python request, the response body is written to a local file:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import requests

r = requests.get(
    "https://api.screenshotneo.com/v1/shot",
    params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"},
    timeout=90,
)
open("shot.webp", "wb").write(r.content)

For Node.js, use the same endpoint and query parameters:

const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);

ScreenshotNeo’s free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 screenshots. Every listed feature is available on every plan. The API is for website captures, so keep PyAutoGUI, MSS, or ImageGrab for desktop pixels and application windows. Sign up for 1,000 free screenshots a month with no card.

Frequently Asked Questions

Can I capture a window that is behind other windows?

A normal screen or region capture records the pixels visible on the display; running the script in the background does not by itself reveal an obscured application. Pillow documents a window-specific ImageGrab option on Windows and macOS with version-specific support, but verify that behavior on the actual system and application.

Does ScreenshotNeo take a screenshot of my computer screen?

No. ScreenshotNeo captures web pages from a URL. Use a desktop capture library when you need pixels from the machine’s display or an application window.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.