Skip to content

Screenshot utilities

conatus.utils.browser.post_processing.screenshot

📸 Utilities to take screenshots of a webpage.

Heavily uses the Pillow library.

get_img_screenshot_async async

get_img_screenshot_async(pw_page: Page) -> Image

Take a screenshot of the page from a Playwright page object.

Note: The expected type for a Playwright page is playwright.async_api.Page.

PARAMETER DESCRIPTION
pw_page

Playwright page object.

TYPE: Page

RETURNS DESCRIPTION
Image

Image.Image: Screenshot of the page.

Source code in conatus/utils/browser/post_processing/screenshot.py
async def get_img_screenshot_async(pw_page: PWPage) -> Image.Image:
    """Take a screenshot of the page from a Playwright page object.

    _Note_: The expected type for a Playwright page is
    `playwright.async_api.Page`.

    Args:
        pw_page: Playwright page object.

    Returns:
        Image.Image: Screenshot of the page.

    """
    screenshot = await pw_page.screenshot()
    return Image.open(io.BytesIO(screenshot)).convert("RGB")

get_img_screenshot

get_img_screenshot(pw_page: Page) -> Image

Take a screenshot of the page from a Playwright page object.

PARAMETER DESCRIPTION
pw_page

Playwright page object.

TYPE: Page

RETURNS DESCRIPTION
Image

Image.Image: Screenshot of the page.

Source code in conatus/utils/browser/post_processing/screenshot.py
def get_img_screenshot(pw_page: PWPage) -> Image.Image:
    """Take a screenshot of the page from a Playwright page object.

    Args:
        pw_page: Playwright page object.

    Returns:
        Image.Image: Screenshot of the page.
    """
    return run_async(get_img_screenshot_async(pw_page), loop=pw_page._loop)  # noqa: SLF001 # pyright: ignore[reportPrivateUsage, reportAny]

get_base64_screenshot

get_base64_screenshot(
    img: Image, img_res: int = DEFAULT_SCREENSHOT_RESOLUTION
) -> str

Convert the screenshot to base64.

Note the use of img_res to resize the image. The equation is img_res x img_res * H / W. This means, for instance, that if the image is 1920 x 1080, and img_res is 500, the image will be resized to 500 x 281.

PARAMETER DESCRIPTION
img

Screenshot of the page.

TYPE: Image

img_res

Resolution of the image. The image will be resized to img_res x img_res * H / W. Defaults to 1024.

TYPE: int DEFAULT: DEFAULT_SCREENSHOT_RESOLUTION

RETURNS DESCRIPTION
str

Base64 encoded screenshot of the page.

TYPE: str

Source code in conatus/utils/browser/post_processing/screenshot.py
def get_base64_screenshot(
    img: Image.Image, img_res: int = DEFAULT_SCREENSHOT_RESOLUTION
) -> str:
    """Convert the screenshot to base64.

    Note the use of `img_res` to resize the image. The equation is `img_res` x
    `img_res * H / W`. This means, for instance, that if the image is 1920 x
    1080, and `img_res` is 500, the image will be resized to 500 x 281.

    Args:
        img (PIL.Image.Image): Screenshot of the page.
        img_res (int): Resolution of the image. The image will be resized to
            `img_res` x `img_res * H / W`. Defaults to 1024.

    Returns:
        str: Base64 encoded screenshot of the page.
    """
    width, height = img.size
    img = img.resize((img_res, int(img_res * height / width)))
    buffer = BytesIO()
    img.save(buffer, format="JPEG")
    return base64.b64encode(buffer.getvalue()).decode("utf-8")

get_screenshots_async async

get_screenshots_async(
    page: Page | Page,
    img_res: int = DEFAULT_SCREENSHOT_RESOLUTION,
) -> tuple[Image, str]

Get two screenshots (Img + b64) of the (Conatus or Playwright) page.

Note: The expected type for a Playwright page is playwright.async_api._generated.Page.

PARAMETER DESCRIPTION
page

Page object (either Playwright or Conatus)

TYPE: Page | Page

img_res

Resolution of the image. The image will be resized to img_res x img_res * H / W. Defaults to 1024.

TYPE: int DEFAULT: DEFAULT_SCREENSHOT_RESOLUTION

RETURNS DESCRIPTION
tuple[Image, str]

tuple[Image.Image, str]: The screenshot and base64 encoded screenshot.

Source code in conatus/utils/browser/post_processing/screenshot.py
async def get_screenshots_async(
    page: "PWPage | Page", img_res: int = DEFAULT_SCREENSHOT_RESOLUTION
) -> tuple[Image.Image, str]:
    """Get two screenshots (Img + b64) of the (Conatus or Playwright) page.

    _Note_: The expected type for a Playwright page is
    `playwright.async_api._generated.Page`.

    Args:
        page: Page object (either Playwright or Conatus)
        img_res (int): Resolution of the image. The image will be resized to
            `img_res` x `img_res * H / W`. Defaults to 1024.

    Returns:
        tuple[Image.Image, str]: The screenshot and base64 encoded screenshot.
    """
    pw_page = page if isinstance(page, PWPage) else page.pw_page
    s_shot = await get_img_screenshot_async(pw_page)
    s_shot_b64 = get_base64_screenshot(s_shot, img_res=img_res)
    return s_shot, s_shot_b64

get_screenshots

get_screenshots(
    page: Page | Page,
    img_res: int = DEFAULT_SCREENSHOT_RESOLUTION,
) -> tuple[Image, str]

Get two screenshots (Img + b64) of the (Conatus or Playwright) page.

PARAMETER DESCRIPTION
page

Page object (either Playwright or Conatus)

TYPE: Page | Page

img_res

Resolution of the image. The image will be resized to img_res x img_res * H / W. Defaults to 1024.

TYPE: int DEFAULT: DEFAULT_SCREENSHOT_RESOLUTION

RETURNS DESCRIPTION
tuple[Image, str]

tuple[Image.Image, str]: The screenshot and base64 encoded screenshot.

Source code in conatus/utils/browser/post_processing/screenshot.py
def get_screenshots(
    page: "PWPage | Page", img_res: int = DEFAULT_SCREENSHOT_RESOLUTION
) -> tuple[Image.Image, str]:
    """Get two screenshots (Img + b64) of the (Conatus or Playwright) page.

    Args:
        page: Page object (either Playwright or Conatus)
        img_res (int): Resolution of the image. The image will be resized to
            `img_res` x `img_res * H / W`. Defaults to 1024.

    Returns:
        tuple[Image.Image, str]: The screenshot and base64 encoded screenshot.
    """
    loop = page._loop if isinstance(page, PWPage) else page.pw_page._loop  # noqa: SLF001 # pyright: ignore[reportPrivateUsage, reportAny]
    return run_async(get_screenshots_async(page, img_res), loop=loop)  # pyright: ignore[reportAny]