code: def convert_mask_to_pil(mask, real=True):
      
      from PIL import Image
      header = mask._layer._psd._record.header
      channel_ids = [ci.id for ci in mask._layer._record.channel_info]
      if real and mask._has_real():
          width = mask._data.real_right - mask._data.real_left
          height = mask._data.real_bottom - mask._data.real_top
          channel = mask._layer._channels[
              channel_ids.index(ChannelID.REAL_USER_LAYER_MASK)
          ]
      else:
          width = mask._data.right - mask._data.left
          height = mask._data.bottom - mask._data.top
          channel = mask._layer._channels[
              channel_ids.index(ChannelID.USER_LAYER_MASK)
          ]
      data = channel.get_data(width, height, header.depth, header.version)
      return _create_channel((width, height), data, header.depth)
docs: Convert Mask to PIL Image.