code: def PIL_to_bytes(img, ext: str = "png", quality: int = None):
      """Return the PIL image as byte stream. Useful to send image via requests."""
      bytes_io = io.BytesIO()
      if quality is not None:
          img.save(bytes_io, format=ext, quality=quality)
      else:
          subsampling = -1 if ext.lower() in ["jpg", "jpeg"] else 0
          img.save(
              bytes_io, format=ext, quality=100, subsampling=subsampling
          )
      bytes_io.seek(0)
      return bytes_io
  
imports: import io
  import os
  import random
  import threading
  import time
  from enum import Enum
  from typing import Iterator, List, Optional
  from PIL import JpegImagePlugin
  from lightly.openapi_generated.swagger_client.configuration import Configuration
label_desc: Use image node_id 4 for calling a global function with example usage: PIL_to_bytes(img, ext, quality) and returns: bytes_io