HUGO Docs

  • News
  • About
    • In this section
    • Introduction
    • Features
    • Privacy
    • Security
    • License
  • Installation
    • In this section
    • macOS
    • Linux
    • Windows
    • BSD
  • Getting started
    • In this section
    • Quick start
    • Basic usage
    • Directory structure
    • Configuration
    • Configure markup
    • Glossary of terms
    • External learning resources
  • Quick reference
    • In this section
    • Emojis
    • Functions
    • Methods
    • Page collections
  • Content management
    • In this section
    • Organization
    • Page bundles
    • Content formats
    • Front matter
    • Build options
    • Page resources
    • Image processing
    • Shortcodes
    • Related content
    • Sections
    • Content types
    • Archetypes
    • Taxonomies
    • Summaries
    • Links and cross references
    • URL management
    • Menus
    • Comments
    • Multilingual
    • Markdown attributes
    • Syntax highlighting
    • Diagrams
    • Mathematics
    • Data sources
    • Content adapters
  • Templates
    • In this section
    • Introduction
    • Template types
    • Lookup order
    • Base templates
    • Home templates
    • Single templates
    • Section templates
    • Taxonomy templates
    • Term templates
    • Partial templates
    • Content view templates
    • Shortcode templates
    • Sitemap templates
    • RSS templates
    • 404 templates
    • robots.txt templates
    • Menus
    • Pagination
    • Embedded templates
    • Custom output formats
  • Functions
    • In this section
    • cast
    • collections
    • compare
    • crypto
    • css
    • data
    • debug
    • diagrams
    • encoding
    • fmt
    • global
    • go template
    • hugo
    • images
    • inflect
    • js
    • lang
    • math
    • openapi3
    • os
    • partials
    • path
    • reflect
    • resources
    • safe
    • strings
    • templates
    • time
    • transform
    • urls
  • Methods
    • In this section
    • Duration
    • Menu
    • Menu entry
    • Page
    • Pager
    • Pages
    • Resource
    • Shortcode
    • Site
    • Taxonomy
    • Time
  • Render hooks
    • In this section
    • Introduction
    • Code blocks
    • Headings
    • Images
    • Links
  • Hugo Modules
    • In this section
    • Configure Hugo modules
    • Use Hugo Modules
    • Theme components
  • Hugo Pipes
    • In this section
    • Introduction
    • Transpile Sass to CSS
    • PostCSS
    • PostProcess
    • JavaScript building
    • Babel
    • Asset minification
    • Concatenating assets
    • Fingerprinting and SRI hashing
    • Resource from string
    • Resource from template
  • CLI
  • Troubleshooting
    • In this section
    • Audit
    • Logging
    • Inspection
    • Deprecation
    • Performance
    • FAQs
  • Developer tools
    • In this section
    • Editor plugins
    • Front-ends
    • Search
    • Migrations
    • Other projects
  • Hosting and deployment
    • In this section
    • Hugo Deploy
    • Deploy with Rclone
    • Deploy with Rsync
    • Host on 21YunBox
    • Host on AWS Amplify
    • Host on Azure Static Web Apps
    • Host on Cloudflare Pages
    • Host on Firebase
    • Host on GitHub Pages
    • Host on GitLab Pages
    • Host on KeyCDN
    • Host on Netlify
    • Host on Render
  • Contribute
    • In this section
    • Development
    • Documentation
    • Themes
  • Maintenance
FUNCTIONS IMAGE FUNCTIONS

images.Dither

Returns an image filter that dithers an image.

Syntax

images.Dither [OPTIONS]

Returns

images.filter
New in v0.123.0

Options

colors
(string array) A slice of two or more colors that make up the dithering palette, each expressed as an RGB or RGBA hexadecimal value, with or without a leading hash mark. The default values are opaque black (000000ff) and opaque white (ffffffff).
method
(string) The dithering method. See the dithering methods section below for a list of the available methods. Default is FloydSteinberg.
serpentine
(bool) Applicable to error diffusion dithering methods, serpentine controls whether the error diffusion matrix is applied in a serpentine manner, meaning that it goes right-to-left every other line. This greatly reduces line-type artifacts. Default is true.
strength
(float) The strength at which to apply the dithering matrix, typically a value in the range [0, 1]. A value of 1.0 applies the dithering matrix at 100% strength (no modification of the dither matrix). The strength is inversely proportional to contrast; reducing the strength increases the contrast. Setting strength to a value such as 0.8 can be useful to reduce noise in the dithered image. Default is 1.0.

Usage

Create the options map:

{{ $opts := dict
  "colors" (slice "222222" "808080" "dddddd")
  "method" "ClusteredDot4x4"
  "strength" 0.85
}}

Create the filter:

{{ $filter := images.Dither $opts }}

Or create the filter using the default settings:

{{ $filter := images.Dither }}

Apply the filter using the images.Filter function:

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

You can also apply the filter using 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 }}

Dithering methods

See the Go documentation for descriptions of each of the dithering methods below.

Error diffusion dithering methods:

  • Atkinson
  • Burkes
  • FalseFloydSteinberg
  • FloydSteinberg
  • JarvisJudiceNinke
  • Sierra
  • Sierra2
  • Sierra2_4A
  • Sierra3
  • SierraLite
  • Simple2D
  • StevenPigeon
  • Stucki
  • TwoRowSierra

Ordered dithering methods:

  • ClusteredDot4x4
  • ClusteredDot6x6
  • ClusteredDot6x6_2
  • ClusteredDot6x6_3
  • ClusteredDot8x8
  • ClusteredDotDiagonal16x16
  • ClusteredDotDiagonal6x6
  • ClusteredDotDiagonal8x8
  • ClusteredDotDiagonal8x8_2
  • ClusteredDotDiagonal8x8_3
  • ClusteredDotHorizontalLine
  • ClusteredDotSpiral5x5
  • ClusteredDotVerticalLine
  • Horizontal3x5
  • Vertical5x3

Example

This example uses the default dithering options.

Original

Zion National Park

Processed

Zion National Park

Recommendations

Regardless of dithering method, do both of the following to obtain the best results:

  1. Scale the image before dithering
  2. Output the image to a lossless format such as GIF or PNG

The example below does both of these, and it sets the dithering palette to the three most dominant colors in the image.

{{ with resources.Get "original.jpg" }}
  {{ $opts := dict
    "method" "ClusteredDotSpiral5x5"
    "colors" (first 3 .Colors)
  }}
  {{ $filters := slice
    (images.Process "resize 800x")
    (images.Dither $opts)
    (images.Process "png")
  }}
  {{ with . | images.Filter $filters }}
    <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
  {{ end }}
{{ end }}

For best results, if the dithering palette is grayscale, convert the image to grayscale before dithering.

{{ $opts := dict "colors" (slice "222" "808080" "ddd") }}
{{ $filters := slice
  (images.Process "resize 800x")
  (images.Grayscale)
  (images.Dither $opts)
  (images.Process "png")
}}
{{ with images.Filter $filters . }}
  <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}

The example above:

  1. Resizes the image to be 800 px wide
  2. Converts the image to grayscale
  3. Dithers the image using the default (FloydSteinberg) dithering method with a grayscale palette
  4. Converts the image to the PNG format

See also

  • images.Filter
  • images.Process
  • Colors
  • Filter

On this page

  • Options
  • Usage
  • Dithering methods
  • Example
  • Recommendations

In this section

  • images.AutoOrient
  • images.Brightness
  • images.ColorBalance
  • images.Colorize
  • images.Config
  • images.Contrast
  • images.Dither
  • images.Filter
  • images.Gamma
  • images.GaussianBlur
  • images.Grayscale
  • images.Hue
  • images.Invert
  • images.Opacity
  • images.Overlay
  • images.Padding
  • images.Pixelate
  • images.Process
  • images.Saturation
  • images.Sepia
  • images.Sigmoid
  • images.Text
  • images.UnsharpMask
Last updated: July 5, 2024: hugo doc (a0ca0ab)
Improve this page
 

The Hugo logos are copyright © Steve Francia 2013–2024.

The Hugo Gopher is based on an original work by Renée French.

  • News
  • About
  • Installation
  • Getting started
  • Quick reference
  • Content management
  • Templates
  • Functions
  • Methods
  • Render hooks
  • Hugo Modules
  • Hugo Pipes
  • CLI
  • Troubleshooting
  • Developer tools
  • Hosting and deployment
  • Contribute
  • Maintenance