code: def rasterize_pdf(
      input_file: os.PathLike,
      output_file: os.PathLike,
      *,
      raster_device: str,
      raster_dpi: Resolution,
      pageno: int = 1,
      page_dpi: Resolution | None = None,
      rotation: int | None = None,
      filter_vector: bool = False,
      stop_on_error: bool = False,
  ):
      """Rasterize one page of a PDF at resolution raster_dpi in canvas units."""
      raster_dpi = raster_dpi.round(6)
      if not page_dpi:
          page_dpi = raster_dpi
  
      args_gs = (
          [
              GS,
              "-dQUIET",
              "-dSAFER",
              "-dBATCH",
              "-dNOPAUSE",
              "-dInterpolateControl=-1",
              f"-sDEVICE={raster_device}",
              f"-dFirstPage={pageno}",
              f"-dLastPage={pageno}",
              f"-r{raster_dpi.x:f}x{raster_dpi.y:f}",
          ]
          + (["-dFILTERVECTOR"] if filter_vector else [])
          + (["-dPDFSTOPONERROR"] if stop_on_error else [])
          + [
              "-o",
              "-",
              "-sstdout=%stderr",  # Literal %s, not string interpolation
              "-dAutoRotatePages=/None",  # Probably has no effect on raster
              "-f",
              fspath(input_file),
          ]
      )
  
      try:
          p = run(args_gs, stdout=PIPE, stderr=PIPE, check=True)
      except CalledProcessError as e:
          log.error(e.stderr.decode(errors="replace"))
          raise SubprocessOutputError(
              "Ghostscript rasterizing failed"
          ) from e
      else:
          stderr = p.stderr.decode(errors="replace")
          if _gs_error_reported(stderr):
              log.error(stderr)
  
      try:
          with Image.open(BytesIO(p.stdout)) as im:
              if rotation is not None:
                  log.debug("Rotating output by %i", rotation)
                  # rotation is a clockwise angle and Image.ROTATE_* is
                  # counterclockwise so this cancels out the rotation
                  if rotation == 90:
                      im = im.transpose(Image.Transpose.ROTATE_90)
                  elif rotation == 180:
                      im = im.transpose(Image.Transpose.ROTATE_180)
                  elif rotation == 270:
                      im = im.transpose(Image.Transpose.ROTATE_270)
                  if rotation % 180 == 90:
                      page_dpi = page_dpi.flip_axis()
              im.save(fspath(output_file), dpi=page_dpi)
      except UnidentifiedImageError:
          log.error(
              f"Ghostscript (using {raster_device} at {raster_dpi} dpi) produced "
              "an invalid page image file."
          )
          raise
  
imports: from __future__ import annotations
  import logging
  import os
  import re
  from collections import deque
  from io import BytesIO
  from os import fspath
  from pathlib import Path
  from subprocess import PIPE, CalledProcessError
  from packaging.version import Version
  from PIL import Image, UnidentifiedImageError
  from ocrmypdf.exceptions import ColorConversionNeededError, SubprocessOutputError
  from ocrmypdf.helpers import Resolution
  from ocrmypdf.subprocess import get_version, run, run_polling_stderr
label_desc: Use image node_id 4 for calling a global function with example usage: rasterize_pdf(input_file, output_file) without return types