code: def extract_image_generic(
      *, pdf: Pdf, root: Path, image: Stream, xref: Xref, options
  ) -> XrefExt | None:
      """Generic image extraction."""
      result = extract_image_filter(image, xref)
      if result is None:
          return None
      pim, filtdp = result
  
      # Don't try to PNG-optimize 1bpp images, since JBIG2 does it better.
      if pim.bits_per_component == 1:
          return None
  
      if filtdp[0] == Name.DCTDecode and options.optimize >= 2:
          # This is a simple heuristic derived from some training data, that has
          # about a 70% chance of guessing whether the JPEG is high quality,
          # and possibly recompressible, or not. The number itself doesn't mean
          # anything.
          # bytes_per_pixel = int(raw_jpeg.Length) / (w * h)
          # jpeg_quality_estimate = 117.0 * (bytes_per_pixel ** 0.213)
          # if jpeg_quality_estimate < 65:
          #     return None
          try:
              imgname = root / f"{xref:08d}"
              with imgname.open("wb") as f:
                  ext = pim.extract_to(stream=f)
              imgname.rename(imgname.with_suffix(ext))
          except UnsupportedImageTypeError:
              return None
          return XrefExt(xref, ext)
      elif (
          pim.indexed
          and pim.colorspace in pim.SIMPLE_COLORSPACES
          and options.optimize >= 3
      ):
          # Try to improve on indexed images - these are far from low hanging
          # fruit in most cases
          pim.as_pil_image().save(png_name(root, xref))
          return XrefExt(xref, ".png")
      elif not pim.indexed and pim.colorspace in pim.SIMPLE_COLORSPACES:
          # An optimization opportunity here, not currently taken, is directly
          # generating a PNG from compressed data
          try:
              pim.as_pil_image().save(png_name(root, xref))
          except NotImplementedError:
              log.warning(
                  "PDF contains an atypical image that cannot be optimized."
              )
              return None
          return XrefExt(xref, ".png")
      elif (
          not pim.indexed
          and pim.colorspace == Name.ICCBased
          and pim.bits_per_component == 1
          and not options.jbig2_lossy
      ):
          # We can losslessly optimize 1-bit images to CCITT or JBIG2 without
          # paying any attention to the ICC profile, provided we're not doing
          # lossy JBIG2
          pim.as_pil_image().save(png_name(root, xref))
          return XrefExt(xref, ".png")
  
      return None
  
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 6 for calling a global function with example usage: extract_image_generic() and returns: None, None, None, XrefExt, XrefExt, None, XrefExt, XrefExt, None