def apply_mask(image, mask, invert=False):
    image.alpha_channel = True
if invert:
    mask.negate()
with Image(width=image.width, height=image.height, background=Color(
    'transparent')) as alpha_image:
    alpha_image.composite_channel('alpha', mask, 'copy_opacity', 0, 0)
image.composite_channel('alpha', alpha_image, 'multiply', 0, 0)
Composition Masking using Python Wand