HUGO
Menu
GitHub 90529 stars Mastodon

images.Opacity

Returns an image filter that changes the opacity of an image.
Syntax
images.Opacity OPACITY
Returns
images.filter

Usage

The images.Opacity function returns an image filter that changes the opacity of an image.

The filter is most useful when the target format supports transparency, such as AVIF, PNG, or WebP. If the image has a different format, combine this filter with an images.Process filter that converts the image to one of these formats.

Arguments

OPACITY
(float) The opacity, which must be in the range [0, 1]. A value of 0 produces a transparent image, a value of 0.5 produces a semi-transparent image, and a value of 1 produces an opaque image.

Examples

{{ $filter := images.Opacity 0.65 }}

Applying the filter with the images.Filter function:

{{ with resources.Get "images/original.jpg" }}
  {{ with . | images.Filter $filter }}
    <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
  {{ end }}
{{ end }}

Applying the filter with the Filter method on a Resource object:

{{ with resources.Get "images/original.jpg" }}
  {{ with .Filter $filter }}
    <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
  {{ end }}
{{ end }}

This is the original image:

Zion National Park

This is the processed image:

Zion National Park

Converting the image to PNG to support transparency:

{{ with resources.Get "images/original.jpg" }}
  {{ $filters := slice
    (images.Opacity 0.65)
    (images.Process "png")
  }}
  {{ with . | images.Filter $filters }}
    <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
  {{ end }}
{{ end }}