code: def _save(im, fp, filename):
      fp.write(_MAGIC)  # (2+2)
      bmp = im.encoderinfo.get("bitmap_format") == "bmp"
      sizes = im.encoderinfo.get(
          "sizes",
          [
              (16, 16),
              (24, 24),
              (32, 32),
              (48, 48),
              (64, 64),
              (128, 128),
              (256, 256),
          ],
      )
      frames = []
      provided_ims = [im] + im.encoderinfo.get("append_images", [])
      width, height = im.size
      for size in sorted(set(sizes)):
          if (
              size[0] > width
              or size[1] > height
              or size[0] > 256
              or size[1] > 256
          ):
              continue
  
          for provided_im in provided_ims:
              if provided_im.size != size:
                  continue
              frames.append(provided_im)
              if bmp:
                  bits = BmpImagePlugin.SAVE[provided_im.mode][1]
                  bits_used = [bits]
                  for other_im in provided_ims:
                      if other_im.size != size:
                          continue
                      bits = BmpImagePlugin.SAVE[other_im.mode][1]
                      if bits not in bits_used:
                          # Another image has been supplied for this size
                          # with a different bit depth
                          frames.append(other_im)
                          bits_used.append(bits)
              break
          else:
              # TODO: invent a more convenient method for proportional scalings
              frame = provided_im.copy()
              frame.thumbnail(
                  size, Image.Resampling.LANCZOS, reducing_gap=None
              )
              frames.append(frame)
      fp.write(o16(len(frames)))  # idCount(2)
      offset = fp.tell() + len(frames) * 16
      for frame in frames:
          width, height = frame.size
          # 0 means 256
          fp.write(o8(width if width < 256 else 0))  # bWidth(1)
          fp.write(o8(height if height < 256 else 0))  # bHeight(1)
  
          bits, colors = (
              BmpImagePlugin.SAVE[frame.mode][1:] if bmp else (32, 0)
          )
          fp.write(o8(colors))  # bColorCount(1)
          fp.write(b"\0")  # bReserved(1)
          fp.write(b"\0\0")  # wPlanes(2)
          fp.write(o16(bits))  # wBitCount(2)
  
          image_io = BytesIO()
          if bmp:
              frame.save(image_io, "dib")
  
              if bits != 32:
                  and_mask = Image.new("1", size)
                  ImageFile._save(
                      and_mask,
                      image_io,
                      [("raw", (0, 0) + size, 0, ("1", 0, -1))],
                  )
          else:
              frame.save(image_io, "png")
          image_io.seek(0)
          image_bytes = image_io.read()
          if bmp:
              image_bytes = (
                  image_bytes[:8] + o32(height * 2) + image_bytes[12:]
              )
          bytes_len = len(image_bytes)
          fp.write(o32(bytes_len))  # dwBytesInRes(4)
          fp.write(o32(offset))  # dwImageOffset(4)
          current = fp.tell()
          fp.seek(offset)
          fp.write(image_bytes)
          offset = offset + bytes_len
          fp.seek(current)
  
imports: from __future__ import annotations
  import warnings
  from io import BytesIO
  from math import ceil, log
  from .None import BmpImagePlugin, Image, ImageFile, PngImagePlugin
  from ._binary import i16le
  from ._binary import i32le
  from ._binary import o8
  from ._binary import o16le
  from ._binary import o32le
label_desc: Use image node_id 1 for calling a global function with example usage: _save(im, fp, filename) without return types