code: def _save(im, fp, filename):
      if im.mode not in ("RGB", "RGBA", "L", "LA"):
          msg = f"cannot write mode {im.mode} as DDS"
          raise OSError(msg)
  
      alpha = im.mode[-1] == "A"
      if im.mode[0] == "L":
          pixel_flags = DDPF.LUMINANCE
          rawmode = im.mode
          if alpha:
              rgba_mask = [0x000000FF, 0x000000FF, 0x000000FF]
          else:
              rgba_mask = [0xFF000000, 0xFF000000, 0xFF000000]
      else:
          pixel_flags = DDPF.RGB
          rawmode = im.mode[::-1]
          rgba_mask = [0x00FF0000, 0x0000FF00, 0x000000FF]
  
          if alpha:
              r, g, b, a = im.split()
              im = Image.merge("RGBA", (a, r, g, b))
      if alpha:
          pixel_flags |= DDPF.ALPHAPIXELS
      rgba_mask.append(0xFF000000 if alpha else 0)
  
      flags = (
          DDSD.CAPS
          | DDSD.HEIGHT
          | DDSD.WIDTH
          | DDSD.PITCH
          | DDSD.PIXELFORMAT
      )
      bitcount = len(im.getbands()) * 8
      pitch = (im.width * bitcount + 7) // 8
  
      fp.write(
          o32(DDS_MAGIC)
          + struct.pack(
              "<7I",
              124,  # header size
              flags,  # flags
              im.height,
              im.width,
              pitch,
              0,  # depth
              0,  # mipmaps
          )
          + struct.pack("11I", *((0,) * 11))  # reserved
          # pfsize, pfflags, fourcc, bitcount
          + struct.pack("<4I", 32, pixel_flags, 0, bitcount)
          + struct.pack("<4I", *rgba_mask)  # dwRGBABitMask
          + struct.pack("<5I", DDSCAPS.TEXTURE, 0, 0, 0, 0)
      )
      ImageFile._save(
          im,
          fp,
          [
              ImageFile._Tile(
                  "raw", (0, 0) + im.size, 0, (rawmode, 0, 1)
              )
          ],
      )
  
imports: from __future__ import annotations
  import io
  import struct
  import sys
  from enum import IntEnum, IntFlag
  from .None import Image, ImageFile, ImagePalette
  from ._binary import i32le
  from ._binary import o8
  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