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

Use the right reduction method for the result you need. Selenium’s standard WebDriver screenshot is a lossless PNG of the current visual viewport. To capture fewer pixels, set the browser window or viewport before calling screenshot(). To keep the same rendering but create a smaller file, save the PNG and resize or re-encode it with an image library. Selenium’s screenshot API does not provide a quality or JPEG-compression argument.

Those operations are not interchangeable: changing the viewport can trigger responsive breakpoints, while post-processing changes only the saved image.

What “smaller” means in Selenium

There are two separate targets:

  • Fewer pixels at capture time: request a smaller browser window or viewport before the page is rendered and captured.
  • A smaller output file: keep the capture, then resize the raster image and/or encode it as JPEG or WebP.

The WebDriver specification describes screenshots as a snapshot of the visual viewport’s framebuffer encoded as a lossless PNG. Selenium’s Python binding exposes that PNG as a file, bytes, or base64 data. It does not expose a screenshot-quality setting.

Choose the first method when you actually want the page to render at a smaller device size. Choose the second when you need the desktop layout preserved but want less storage or transfer overhead.

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

Reduce captured dimensions with the browser window

Set the size before navigation or capture

In Selenium Python, set_window_size(width, height) takes pixel values. Set it before the screenshot so responsive CSS, JavaScript layout code and lazy-loading behavior see the intended size.

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument("--headless=new")
options.add_argument("--disable-gpu")

driver = webdriver.Chrome(options=options)
try:
    driver.set_window_size(1024, 700)
    driver.get("https://example.com")
    driver.save_screenshot("viewport-1024x700.png")
finally:
    driver.quit()

The resulting image is the visual viewport exposed by the browser, not necessarily exactly 1,024 × 700 pixels. Browser chrome, headless mode, device-pixel ratio, driver behavior and operating-system limits can affect the observed bitmap. Inspect the file rather than assuming the requested outer-window size maps one-to-one to image pixels.

Use a mobile-sized viewport deliberately

A smaller width can activate a mobile navigation menu, stack columns, hide controls or change font sizes. That is useful for responsive testing, but it is not a neutral way to shrink a desktop screenshot. If the page’s desktop composition must remain unchanged, capture at the original size and post-process the image instead.

driver.set_window_size(390, 844)
driver.get("https://example.com")
driver.save_screenshot("mobile-viewport.png")

Validate the actual dimensions

Use Pillow (Python Imaging Library) after capture to report the bitmap dimensions:

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

with Image.open("viewport-1024x700.png") as image:
    print(image.size)  # (width, height) in image pixels

Automated pipelines should assert the observed size and record it with the test artifact. This catches differences between local headed runs, CI headless runs and machines with different display scaling.

Reduce dimensions after capture with Pillow

Resize while preserving the original PNG

Install Pillow with python -m pip install pillow. Keep the original PNG when pixel-accurate comparison, archival evidence or later processing matters, and create a derivative at the target dimensions.

from PIL import Image

source = "viewport-1024x700.png"
with Image.open(source) as image:
    target_width = 800
    target_height = round(image.height * target_width / image.width)
    smaller = image.resize((target_width, target_height), Image.Resampling.LANCZOS)
    smaller.save("viewport-800px.png", format="PNG", optimize=True)

This changes the image artifact, not the layout that the browser rendered. The aspect ratio is retained by calculating the height from the original dimensions. If you need an exact box instead, pass both target dimensions, accepting that the image may be stretched unless you crop first.

Fit inside a maximum box without distortion

from PIL import Image

with Image.open("viewport-1024x700.png") as image:
    image.thumbnail((800, 600), Image.Resampling.LANCZOS)
    image.save("viewport-fit.png", format="PNG", optimize=True)

thumbnail() modifies the in-memory image in place and never enlarges it. The output fits within 800 × 600 while retaining its aspect ratio.

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

Reduce file size by changing the output format

JPEG for photographic or visually tolerant content

JPEG is lossy: it can be substantially smaller, but text edges, thin borders and UI icons may show artifacts. Set an explicit quality and keep the PNG if exact pixels matter.

from PIL import Image

with Image.open("viewport-1024x700.png") as image:
    rgb = image.convert("RGB")
    rgb.save("viewport-q75.jpg", format="JPEG", quality=75, optimize=True, progressive=True)

PNG screenshots often contain large areas of flat color and sharp text, so JPEG’s artifacts can be noticeable. There is no universal quality value that is correct for every page; inspect representative images and choose a documented setting for your application.

WebP as an alternative derivative

from PIL import Image

with Image.open("viewport-1024x700.png") as image:
    image.save("viewport-q80.webp", format="WEBP", quality=80, method=6)

WebP supports lossy and lossless modes. Verify that every consumer of the screenshot accepts the chosen format before replacing PNG in a pipeline.

Preserve transparency when it is meaningful

PNG can retain an alpha channel. JPEG cannot, so converting an RGBA screenshot to JPEG requires compositing it onto a background color first. For a transparent PNG, use:

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.
from PIL import Image

with Image.open("transparent.png") as image:
    rgba = image.convert("RGBA")
    background = Image.new("RGBA", rgba.size, (255, 255, 255, 255))
    background.alpha_composite(rgba)
    background.convert("RGB").save("opaque.jpg", quality=80, optimize=True)

Save bytes or base64 when you need programmatic processing

Selenium Python provides more than a file-writing shortcut. Use get_screenshot_as_png() for bytes and get_screenshot_as_base64() when an API or message format requires base64.

from io import BytesIO
from PIL import Image
from selenium import webdriver

 driver = webdriver.Chrome()
try:
    driver.set_window_size(1024, 700)
    driver.get("https://example.com")
    png_bytes = driver.get_screenshot_as_png()
    with Image.open(BytesIO(png_bytes)) as image:
        image.thumbnail((800, 600), Image.Resampling.LANCZOS)
        image.save("processed.webp", format="WEBP", quality=80)
finally:
    driver.quit()

Remove the accidental leading space before driver if you copy this into a file; the complete runnable version is:

from io import BytesIO
from PIL import Image
from selenium import webdriver

driver = webdriver.Chrome()
try:
    driver.set_window_size(1024, 700)
    driver.get("https://example.com")
    png_bytes = driver.get_screenshot_as_png()
    with Image.open(BytesIO(png_bytes)) as image:
        image.thumbnail((800, 600), Image.Resampling.LANCZOS)
        image.save("processed.webp", format="WEBP", quality=80)
finally:
    driver.quit()

Viewport screenshots versus full-page images

The standard screenshot command captures the visual viewport. A full-document screenshot is a separate capability and is not uniform across browsers and language bindings. Selenium’s current Python Firefox binding documents full-document screenshot methods, but you should confirm support for the exact browser, driver and binding in your environment before depending on them.

Do not confuse a full-page capture with a lower-quality capture. A full-page image normally contains more pixels and can be reduced afterward using the same Pillow workflow. If you need a specific viewport layout, set the window first; if you need the entire document, use a documented full-page method supported by your selected binding.

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

Choosing the correct workflow

Goal Recommended operation What changes Main risk
Test or show a mobile layout Set a mobile-sized window before navigation Browser rendering and output pixels Responsive breakpoints change the page
Keep desktop layout, fewer pixels Capture at the intended desktop size, then resize Saved image dimensions Small text can become harder to read
Reduce transfer/storage size Encode a derivative as optimized PNG, JPEG or WebP File representation Lossy formats can add artifacts
Pixel-accurate regression testing Retain the original PNG Nothing in the source artifact Larger files

Common problems and fixes

The screenshot is not the requested width or height

WebDriver permits implementations to clamp window dimensions, and some environments cannot resize in single-pixel increments. Check the actual image dimensions with Pillow. Also compare headed and headless runs, device-pixel ratio and display scaling. Treat the requested size as an input, not a guarantee.

The page looks different after reducing the window

This is expected when responsive CSS or JavaScript reacts to the new viewport. Capture at the original layout size and resize the bitmap if visual fidelity to the desktop rendering is the requirement.

The file is still large after resizing

Confirm that the derivative was saved rather than the original PNG. Use PNG optimization, reduce pixel dimensions further, or test JPEG/WebP at an explicit quality. Measure file sizes on representative pages; flat UI screenshots and photographic pages compress differently.

JPEG output has halos or fuzzy text

JPEG is lossy. Increase its quality, use WebP, or keep PNG for interfaces with small text and sharp lines. Do not use a lossy derivative for pixel-diff baselines unless your comparison process is designed for it.

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

Transparency disappears

JPEG has no alpha channel. Composite onto a known background before encoding, or keep a PNG/WebP format that preserves transparency.

A full-page call fails or behaves differently by browser

Full-document screenshot methods are binding- and browser-specific. Verify the method in the Selenium documentation for your exact environment and provide a viewport fallback when portability is more important than one tall image.

The screenshot captures a loading state

Resizing does not wait for application rendering. Add an explicit Selenium wait for a page condition that represents readiness, then capture. Avoid treating a fixed sleep as proof that network requests and client rendering have finished.

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

Or skip the browser setup

ScreenshotNeo provides a one-request screenshot API when you do not want to maintain WebDriver and browser setup. It can return PNG, JPEG, WebP or PDF, and its options include viewport and device presets, retina scale, full-page capture, element selection, resizing, custom CSS and JavaScript, waits, headers, cookies, user agents, geolocation and more.

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

Example cURL (the API documentation has the complete parameter reference):

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

Python:

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)

Node.js:

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

Before capture, ScreenshotNeo accepts cookie and consent banners and removes more than 60 known consent platforms, newsletter popups and chat widgets; each step can be disabled. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads and cache hits are not billed, and response headers identify the page verdict and whether it was billed. Its MCP server lets Claude, Cursor and other MCP clients call take_screenshot, get_page_info and capture_pdf. The Free plan includes 1,000 screenshots each month with no card; paid plans start at $5 for 3,000 shots. Create a free ScreenshotNeo account.

Operational and cost considerations

Browser-based capture includes the cost of starting and managing a browser, driver compatibility and environment-specific rendering differences. Post-processing adds CPU and memory proportional to the image size. Process large screenshots in a streaming or queued worker when possible, and delete intermediate derivatives after delivery if retention is not required.

For visual regression, store the original PNG and generate display-sized derivatives separately. For email, documentation or API responses, define a maximum width, format and quality in configuration so outputs remain consistent across runs. Record the requested viewport and observed image dimensions alongside each artifact.

Free tools Windows power users keep installed

One-click scans. No signup required.

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

Frequently Asked Questions

Does Selenium have a screenshot quality parameter?

No. The standard screenshot is a lossless PNG. Use an image-processing library after capture when you need resizing or lossy encoding.

Can I guarantee that set_window_size() produces those exact screenshot pixels?

No. Browser and operating-system implementations may clamp dimensions or use different pixel mappings. Measure the saved image in the environment that runs your tests.

What should I use for pixel-perfect comparison?

Keep the original PNG and compare at the same observed dimensions. Generate JPEG or WebP only for delivery copies, not the comparison baseline.

Is Selenium full-page screenshot support universal?

No. Full-document methods are browser- and binding-specific; the standard visual-viewport screenshot is the portable baseline.

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.