code: def extract_images(
      pdf: Pdf,
      root: Path,
      options,
      extract_fn: Callable[..., XrefExt | None],
  ) -> Iterator[tuple[int, XrefExt]]:
      """Extract image using extract_fn.
  
      Enumerate images on each page, lookup their xref/ID number in the PDF.
      Exclude images that are soft masks (i.e. alpha transparency related).
      Record the page number on which an image is first used, since images may be
      used on multiple pages (or multiple times on the same page).
  
      Current we do not check Form XObjects or other objects that may contain
      images, and we don't evaluate alternate images or thumbnails.
  
      extract_fn must decide if wants to extract the image in this context. If
      it does a tuple should be returned: (xref, ext) where .ext is the file
      extension. extract_fn must also extract the file it finds interesting.
      """
      errors = 0
      working_xrefs, pageno_for_xref = _find_image_xrefs(pdf)
      for xref in working_xrefs:
          image = pdf.get_object((xref, 0))
          try:
              result = extract_fn(
                  pdf=pdf,
                  root=root,
                  image=image,
                  xref=xref,
                  options=options,
              )
          except Exception:  # pylint: disable=broad-except
              log.exception(
                  f"xref {xref}: While extracting this image, an error occurred"
              )
              errors += 1
          else:
              if result:
                  _, ext = result
                  yield pageno_for_xref[xref], XrefExt(xref, ext)
  
imports: from __future__ import annotations
  import logging
  import sys
  import tempfile
  import threading
  from collections import defaultdict
  from collections.abc import Iterator, MutableSet, Sequence
  from os import fspath
  from pathlib import Path
  from typing import Any, Callable, NamedTuple, NewType
  from warnings import warn
  from zlib import compress
  import img2pdf
  from pikepdf import Dictionary, Name, Object, ObjectStreamMode, Pdf, PdfError, PdfImage, Stream, UnsupportedImageTypeError
  from PIL import Image
  from ocrmypdf._concurrent import Executor, SerialExecutor
  from ocrmypdf._exec import jbig2enc, pngquant
  from ocrmypdf._jobcontext import PdfContext
  from ocrmypdf._progressbar import ProgressBar
  from ocrmypdf.exceptions import OutputFileAccessError
  from ocrmypdf.helpers import IMG2PDF_KWARGS, safe_symlink
label_desc: Use image node_id 9 for calling a global function with example usage: extract_images(pdf, root, options, extract_fn) without return types