code: def _PIL_to_bytes(
      image: PILImage,
      format: ImageFormat = "JPEG",
      quality: int = 100,
  ) -> bytes:
      """Convert a PIL image to bytes."""
      tmp = io.BytesIO()
  
      # User must have specified JPEG, so we must convert it
      if format == "JPEG" and _image_may_have_alpha_channel(image):
          image = image.convert("RGB")
  
      image.save(tmp, format=format, quality=quality)
  
      return tmp.getvalue()
  
imports: import base64
  import io
  import mimetypes
  import re
  from enum import IntEnum
  from typing import TYPE_CHECKING, List, Optional, Sequence, Union, cast
  from urllib.parse import urlparse
  import numpy
  from PIL import GifImagePlugin, Image, ImageFile
  from typing_extensions import Final, Literal, TypeAlias
  from streamlit import runtime
  from streamlit.errors import StreamlitAPIException
  from streamlit.logger import get_logger
  from streamlit.proto.Image_pb2 import ImageList
  from streamlit.runtime import caching
  from streamlit.runtime.metrics_util import gather_metrics
label_desc: Use image node_id 4 for calling a global function with example usage: _PIL_to_bytes(image, format, quality) and returns: tmp