code: def image_to_bytes(image: "PIL.Image.Image") -> bytes:
      """Convert a PIL Image object to bytes using native compression if possible, otherwise use PNG/TIFF compression."""
      buffer = BytesIO()
      if image.format in list_image_compression_formats():
          format = image.format
      else:
          format = (
              "PNG"
              if image.mode in ["1", "L", "LA", "RGB", "RGBA"]
              else "TIFF"
          )
      image.save(buffer, format=format)
      return buffer.getvalue()
  
imports: import os
  import sys
  import warnings
  from dataclasses import dataclass, field
  from io import BytesIO
  from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union
  import numpy
  import pyarrow
  from ..None import config
  from ..download.download_config import DownloadConfig
  from ..download.streaming_download_manager import xopen
  from ..table import array_cast
  from ..utils.file_utils import is_local_path
  from ..utils.py_utils import first_non_null_value, no_op_if_value_is_null, string_to_dict
label_desc: Use image node_id 2 for calling a global function with example usage: image_to_bytes(image) and returns: buffer