# psd_tools.api.mask

Mask module.

Masks are used to determine the visible area of a layer. There are two types of masks: user mask and vector mask. User mask refers to any pixel-based mask, whereas vector mask refers to a mask from a shape path.

Masks are accessible from the layer’s mask property:

    from psd_tools import PSDImage

    psdimage = PSDImage.open('example.psd')
    layer = psdimage[0]
    mask = layer.mask
    assert mask is not None
    print(mask.bbox)  # (left, top, right, bottom)

    mask_image = mask.topil()  # Show the mask as a PIL Image.
    if mask_image is not None:
        mask_image.save("mask.png")

To create a new mask, use the layer’s `create_mask()` method:

    from psd_tools import PSDImage
    from PIL import Image

    psdimage = PSDImage.new(mode="RGB", size=(128, 128))
    layer = psdimage.create_pixel_layer(Image.new("RGB", (128, 128)))
    layer.create_mask(Image.new("L", (128, 128), 255))

Note that creating a pixel layer from RGBA image will automatically create a user mask:

    psdimage = PSDImage.new(mode="RGB", size=(128, 128))
    layer = psdimage.create_pixel_layer(Image.new("RGBA", (128, 128)))

## Mask

class psd_tools.api.mask.Mask(layer: LayerProtocol)

  Mask data attached to a layer.

  There are two distinct internal mask data: user mask and vector mask. User mask refers any pixel-based mask whereas vector mask refers a mask from a shape path. Internally, two masks are combined and referred real mask.

  property background_color: int

    Background color.

  property bbox: tuple[int, int, int, int]

    BBox

  property bottom: int

    Bottom coordinate.

  property data: MaskData

    Return raw mask data, or None if no data.

  property disabled: bool

    Disabled.

  property flags: MaskFlags

    Flags.

  has_real() → bool

    Return True if the mask has real flags.

  property height: int

    Height.

  property left: int

    Left coordinate.

  property parameters: Any

    Parameters.

  property real_flags: MaskFlags | None

    Real flag.

  property right: int

    Right coordinate.

  property size: tuple[int, int]

    (Width, Height) tuple.

  property top: int

    Top coordinate.

  topil(real: bool = True, layer_sized: bool = False, **kwargs: Any) → Image | None

    Get PIL Image of the mask.

    Parameters:

      - real – When True, returns pixel + vector mask combined.

      - layer_sized – When True, returns a layer-sized image pre-filled with `background_color`, with the mask data pasted at the correct position. When False (default), returns the raw stored mask data at mask dimensions.

    Returns:

      PIL Image object, or None if the mask is empty.

  property width: int

    Width.