code: def get_screenshot_as_png(
      obj: UIElement | Document,
      *,
      driver: WebDriver | None = None,
      timeout: int = 5,
      resources: Resources = INLINE,
      width: int | None = None,
      height: int | None = None,
      scale_factor: float = 1,
      state: State | None = None,
  ) -> Image.Image:
      """Get a screenshot of a ``UIElement`` object.
  
      Args:
          obj (UIElement or Document) : a Layout (Row/Column), Plot or Widget
              object or Document to export.
  
          driver (selenium.webdriver) : a selenium webdriver instance to use
              to export the image.
  
          timeout (int) : the maximum amount of time to wait for initialization.
              It will be used as a timeout for loading Bokeh, then when waiting for
              the layout to be rendered.
  
          scale_factor (float, optional) : A factor to scale the output PNG by,
              providing a higher resolution while maintaining element relative
              scales.
  
          state (State, optional) :
              A :class:`State` object. If None, then the current default
              implicit state is used. (default: None).
  
      Returns:
          image (PIL.Image.Image) : a pillow image loaded from PNG.
  
      .. warning::
          Responsive sizing_modes may generate layouts with unexpected size and
          aspect ratios. It is recommended to use the default ``fixed`` sizing mode.
  
      """
      from .webdriver import (
          get_web_driver_device_pixel_ratio,
          scale_factor_less_than_web_driver_device_pixel_ratio,
          webdriver_control,
      )
  
      with _tmp_html() as tmp:
          theme = (state or curstate()).document.theme
          html = get_layout_html(
              obj,
              resources=resources,
              width=width,
              height=height,
              theme=theme,
          )
          with open(tmp.path, mode="w", encoding="utf-8") as file:
              file.write(html)
  
          if driver is not None:
              web_driver = driver
              if not scale_factor_less_than_web_driver_device_pixel_ratio(
                  scale_factor, web_driver
              ):
                  device_pixel_ratio = (
                      get_web_driver_device_pixel_ratio(web_driver)
                  )
                  raise ValueError(
                      f"Expected the web driver to have a device pixel ratio greater than {scale_factor}. "
                      f"Was given a web driver with a device pixel ratio of {device_pixel_ratio}."
                  )
          else:
              web_driver = webdriver_control.get(
                  scale_factor=scale_factor
              )
          web_driver.maximize_window()
          web_driver.get(f"file://{tmp.path}")
          wait_until_render_complete(web_driver, timeout)
          [width, height, dpr] = _maximize_viewport(web_driver)
          png = web_driver.get_screenshot_as_png()
  
      from PIL import Image
  
      return (
          Image.open(io.BytesIO(png))
          .convert("RGBA")
          .crop((0, 0, width * dpr, height * dpr))
          .resize(
              (int(width * scale_factor), int(height * scale_factor))
          )
      )
  
imports: from __future__ import annotations
  import logging
  import io
  import os
  from contextlib import contextmanager
  from os.path import abspath, expanduser, splitext
  from tempfile import mkstemp
  from typing import TYPE_CHECKING, Any, Iterator, cast
  from ..core.types import PathLike
  from ..document import Document
  from ..embed import file_html
  from ..resources import INLINE, Resources
  from ..themes import Theme
  from ..util.warnings import warn
  from .state import State, curstate
  from .util import default_filename
label_desc: Use image node_id 4 for calling a global function with example usage: get_screenshot_as_png(obj) and returns: Image