| 1 |
--- |
| 2 |
title: images.Dither |
| 3 |
description: Returns an image filter that dithers an image. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [] |
| 9 |
returnType: images.filter |
| 10 |
signatures: ['images.Dither [OPTIONS]'] |
| 11 |
--- |
| 12 |
|
| 13 |
## Options |
| 14 |
|
| 15 |
The `images.Dither` filter accepts an options map. |
| 16 |
|
| 17 |
`colors` |
| 18 |
: (`[]string`) 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`). |
| 19 |
|
| 20 |
`method` |
| 21 |
: (`string`) The dithering method. See the [dithering methods](#dithering-methods) section below for a list of the available methods. Default is `FloydSteinberg`. |
| 22 |
|
| 23 |
`serpentine` |
| 24 |
: (`bool`) Applicable to error diffusion dithering methods, whether to apply the error diffusion matrix in a serpentine manner, meaning that it goes right-to-left every other line. This greatly reduces line-type artifacts. Default is `true`. |
| 25 |
|
| 26 |
`strength` |
| 27 |
: (`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`. |
| 28 |
|
| 29 |
## Usage |
| 30 |
|
| 31 |
Create the options map: |
| 32 |
|
| 33 |
```go-html-template |
| 34 |
{{ $opts := dict |
| 35 |
"colors" (slice "222222" "808080" "dddddd") |
| 36 |
"method" "ClusteredDot4x4" |
| 37 |
"strength" 0.85 |
| 38 |
}} |
| 39 |
``` |
| 40 |
|
| 41 |
Create the filter: |
| 42 |
|
| 43 |
```go-html-template |
| 44 |
{{ $filter := images.Dither $opts }} |
| 45 |
``` |
| 46 |
|
| 47 |
Or create the filter using the default settings: |
| 48 |
|
| 49 |
```go-html-template |
| 50 |
{{ $filter := images.Dither }} |
| 51 |
``` |
| 52 |
|
| 53 |
{{% include "/_common/functions/images/apply-image-filter.md" %}} |
| 54 |
|
| 55 |
## Dithering methods |
| 56 |
|
| 57 |
See the [Go documentation][] for descriptions of each of the dithering methods below. |
| 58 |
|
| 59 |
Error diffusion dithering methods: |
| 60 |
|
| 61 |
- Atkinson |
| 62 |
- Burkes |
| 63 |
- FalseFloydSteinberg |
| 64 |
- FloydSteinberg |
| 65 |
- JarvisJudiceNinke |
| 66 |
- Sierra |
| 67 |
- Sierra2 |
| 68 |
- Sierra2_4A |
| 69 |
- Sierra3 |
| 70 |
- SierraLite |
| 71 |
- Simple2D |
| 72 |
- StevenPigeon |
| 73 |
- Stucki |
| 74 |
- TwoRowSierra |
| 75 |
|
| 76 |
Ordered dithering methods: |
| 77 |
|
| 78 |
- ClusteredDot4x4 |
| 79 |
- ClusteredDot6x6 |
| 80 |
- ClusteredDot6x6_2 |
| 81 |
- ClusteredDot6x6_3 |
| 82 |
- ClusteredDot8x8 |
| 83 |
- ClusteredDotDiagonal16x16 |
| 84 |
- ClusteredDotDiagonal6x6 |
| 85 |
- ClusteredDotDiagonal8x8 |
| 86 |
- ClusteredDotDiagonal8x8_2 |
| 87 |
- ClusteredDotDiagonal8x8_3 |
| 88 |
- ClusteredDotHorizontalLine |
| 89 |
- ClusteredDotSpiral5x5 |
| 90 |
- ClusteredDotVerticalLine |
| 91 |
- Horizontal3x5 |
| 92 |
- Vertical5x3 |
| 93 |
|
| 94 |
## Example |
| 95 |
|
| 96 |
This example uses the default dithering options. |
| 97 |
|
| 98 |
{{< img |
| 99 |
src="images/examples/zion-national-park.jpg" |
| 100 |
alt="Zion National Park" |
| 101 |
filter="Dither" |
| 102 |
filterArgs="" |
| 103 |
example=true |
| 104 |
>}} |
| 105 |
|
| 106 |
## Recommendations |
| 107 |
|
| 108 |
Regardless of dithering method, do both of the following to obtain the best results: |
| 109 |
|
| 110 |
1. Scale the image _before_ dithering |
| 111 |
1. Output the image to a lossless format such as GIF or PNG |
| 112 |
|
| 113 |
The example below does both of these, and it sets the dithering palette to the three most dominant colors in the image. |
| 114 |
|
| 115 |
```go-html-template |
| 116 |
{{ with resources.Get "original.jpg" }} |
| 117 |
{{ $opts := dict |
| 118 |
"method" "ClusteredDotSpiral5x5" |
| 119 |
"colors" (first 3 .Colors) |
| 120 |
}} |
| 121 |
{{ $filters := slice |
| 122 |
(images.Process "resize 800x") |
| 123 |
(images.Dither $opts) |
| 124 |
(images.Process "png") |
| 125 |
}} |
| 126 |
{{ with . | images.Filter $filters }} |
| 127 |
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt=""> |
| 128 |
{{ end }} |
| 129 |
{{ end }} |
| 130 |
``` |
| 131 |
|
| 132 |
For best results, if the dithering palette is grayscale, convert the image to grayscale before dithering. |
| 133 |
|
| 134 |
```go-html-template |
| 135 |
{{ $opts := dict "colors" (slice "222" "808080" "ddd") }} |
| 136 |
{{ $filters := slice |
| 137 |
(images.Process "resize 800x") |
| 138 |
(images.Grayscale) |
| 139 |
(images.Dither $opts) |
| 140 |
(images.Process "png") |
| 141 |
}} |
| 142 |
{{ with images.Filter $filters . }} |
| 143 |
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt=""> |
| 144 |
{{ end }} |
| 145 |
``` |
| 146 |
|
| 147 |
The example above: |
| 148 |
|
| 149 |
1. Resizes the image to be 800 px wide |
| 150 |
1. Converts the image to grayscale |
| 151 |
1. Dithers the image using the default (`FloydSteinberg`) dithering method with a grayscale palette |
| 152 |
1. Converts the image to the PNG format |
| 153 |
|
| 154 |
[Go documentation]: https://pkg.go.dev/github.com/makeworld-the-better-one/dither/v2#pkg-variables |
| 155 |
[hexadecimal]: https://developer.mozilla.org/en-US/docs/Web/CSS/hex-color |