code: def convert_layer_to_pil(layer, apply_icc=True, **kwargs):
      
      from PIL import Image
      header = layer._psd._record.header
      if header.color_mode == ColorMode.BITMAP:
          raise NotImplementedError
      width, height = layer.width, layer.height
      channels, alpha = [], None
      for ci, cd in zip(layer._record.channel_info, layer._channels):
          if ci.id in (ChannelID.USER_LAYER_MASK,
                       ChannelID.REAL_USER_LAYER_MASK):
              continue
          channel = cd.get_data(width, height, header.depth, header.version)
          channel_image = _create_channel(
              (width, height), channel, header.depth
          )
          if ci.id == ChannelID.TRANSPARENCY_MASK:
              alpha = channel_image
          else:
              channels.append(channel_image)
      mode = get_pil_mode(header.color_mode.name)
      channels = _check_channels(channels, header.color_mode)
      image = Image.merge(mode, channels)
      if mode == 'CMYK':
          image = image.point(lambda x: 255 - x)
      if alpha is not None:
          if mode in ('RGB', 'L'):
              image.putalpha(alpha)
          else:
              logger.debug('Alpha channel is not supported in %s' % (mode))
  
      if apply_icc and 'ICC_PROFILE' in layer._psd.image_resources:
          image = _apply_icc(
              image, layer._psd.image_resources.get_data('ICC_PROFILE')
          )
      return image
docs: Convert Layer to PIL Image.