code: def rasterize_paper(
      pdf: Union[Path, bytes],
      outpath: Optional[Path] = None,
      dpi: int = 96,
      return_pil=False,
      pages=None,
  ) -> Optional[List[io.BytesIO]]:
      """
      Rasterize a PDF file to PNG images.
  
      Args:
          pdf (Path): The path to the PDF file.
          outpath (Optional[Path], optional): The output directory. If None, the PIL images will be returned instead. Defaults to None.
          dpi (int, optional): The output DPI. Defaults to 96.
          return_pil (bool, optional): Whether to return the PIL images instead of writing them to disk. Defaults to False.
          pages (Optional[List[int]], optional): The pages to rasterize. If None, all pages will be rasterized. Defaults to None.
  
      Returns:
          Optional[List[io.BytesIO]]: The PIL images if `return_pil` is True, otherwise None.
      """
      pils = []
      if outpath is None:
          return_pil = True
      try:
          if isinstance(pdf, (str, Path)):
              pdf = pypdfium2.PdfDocument(pdf)
          if pages is None:
              pages = range(len(pdf))
          renderer = pdf.render(
              pypdfium2.PdfBitmap.to_pil,
              page_indices=pages,
              scale=dpi / 72,
          )
          for i, image in zip(pages, renderer):
              if return_pil:
                  page_bytes = io.BytesIO()
                  image.save(page_bytes, "bmp")
                  pils.append(page_bytes)
              else:
                  image.save((outpath / ("%02d.png" % (i + 1))), "png")
      except Exception as e:
          logging.error(e)
      if return_pil:
          return pils
  
file_path: nougat/nougat/dataset/rasterize.py
imports: import argparse
  import logging
  import pypdfium2
  from pathlib import Path
  from tqdm import tqdm
  import io
  from typing import Optional, List, Union
label_desc: Use image node_id 1 for calling a global function with example usage: rasterize_paper(pdf, outpath, dpi, return_pil, pages) and returns: pils