Debian Codegraph @

docs/content/en/functions/images/Mask.md

raw
1 ---
2 title: images.Mask
3 description: Returns an image filter that applies a mask to the source image.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 aliases: []
9 returnType: images.filter
10 signatures: [images.Mask RESOURCE]
11 ---
12
13 {{< new-in 0.141.0 />}}
14
15 The `images.Mask` filter applies a mask to an image. Black pixels in the mask make the corresponding areas of the base image transparent, while white pixels keep them opaque. Color images are converted to grayscale for masking purposes. The mask is automatically resized to match the dimensions of the base image.
16
17 > [!NOTE]
18 > Of the formats supported by Hugo's imaging pipeline, only AVIF, PNG, and WebP have an alpha channel to support transparency. If your source image has a different format and you require transparent masked areas, convert it to AVIF, PNG, or WebP as shown in the example below.
19
20 When applying a mask to a non-transparent image format such as JPEG, the masked areas will be filled with the color specified by the `bgColor` parameter in your [project configuration][]. You can override that color with a `Process` image filter:
21
22 ```go-html-template
23 {{ $filter := images.Process "#00ff00" }}
24 ```
25
26 ## Usage
27
28 Create a slice of filters, one for format conversion and the other for mask application:
29
30 ```go-html-template
31 {{ $filter1 := images.Process "webp" }}
32 {{ $filter2 := images.Mask (resources.Get "images/mask.png") }}
33 {{ $filters := slice $filter1 $filter2 }}
34 ```
35
36 Apply the filters using the [`images.Filter`][] function:
37
38 ```go-html-template
39 {{ with resources.Get "images/original.jpg" }}
40 {{ with . | images.Filter $filters }}
41 <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
42 {{ end }}
43 {{ end }}
44 ```
45
46 You can also apply the filter using the [`Filter`][] method on a 'Resource' object:
47
48 ```go-html-template
49 {{ with resources.Get "images/original.jpg" }}
50 {{ with .Filter $filters }}
51 <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
52 {{ end }}
53 {{ end }}
54 ```
55
56 ## Example
57
58 Mask
59
60 {{< img
61 src="images/examples/mask.png"
62 example=false
63 >}}
64
65 {{< img
66 src="images/examples/zion-national-park.jpg"
67 alt="Zion National Park"
68 filter="mask"
69 filterArgs="images/examples/mask.png"
70 example=true
71 >}}
72
73 [`Filter`]: /methods/resource/filter/
74 [`images.Filter`]: /functions/images/filter/
75 [project configuration]: /configuration/imaging/