code: def _save(im, fp, tile, bufsize=0):
      """Helper to save image based on tile list
  
      :param im: Image object.
      :param fp: File object.
      :param tile: Tile list.
      :param bufsize: Optional buffer size
      """
  
      im.load()
      if not hasattr(im, "encoderconfig"):
          im.encoderconfig = ()
      tile.sort(key=_tilesort)
      # FIXME: make MAXBLOCK a configuration parameter
      # It would be great if we could have the encoder specify what it needs
      # But, it would need at least the image size in most cases. RawEncode is
      # a tricky case.
      bufsize = max(
          MAXBLOCK, bufsize, im.size[0] * 4
      )  # see RawEncode.c
      try:
          fh = fp.fileno()
          fp.flush()
          _encode_tile(im, fp, tile, bufsize, fh)
      except (AttributeError, io.UnsupportedOperation) as exc:
          _encode_tile(im, fp, tile, bufsize, None, exc)
      if hasattr(fp, "flush"):
          fp.flush()
  
imports: import io
  import itertools
  import struct
  import sys
  from .None import Image
  from ._util import is_path
label_desc: Use image node_id 3 for calling a global function with example usage: _save(im, fp, tile, bufsize) without return types