images.Dither
语法
images.Dither [OPTIONS]
返回值
images.filter
选项
- colors
- (
字符串数组
)构成抖动调色板的两个或多个颜色的切片,每个颜色都表示为 RGB 或 RGBA 十六进制值,带有或不带有前导井号。默认值为不透明黑色 (000000ff
) 和不透明白色 (ffffffff
)。
- method
- (
字符串
)抖动方法。请参阅下面的抖动方法部分,查看可用方法的列表。默认值为FloydSteinberg
。 - serpentine
- (
布尔值
)适用于误差扩散抖动方法,serpentine 控制误差扩散矩阵是否以蛇形方式应用,这意味着它每隔一行从右到左进行。这大大减少了线条类型的伪影。默认值为true
。 - strength
- (
浮点数
)应用抖动矩阵的强度,通常是范围在 [0, 1] 中的值。值为1.0
表示以 100% 的强度应用抖动矩阵(不对抖动矩阵进行修改)。strength
与对比度成反比;降低强度会增加对比度。将strength
设置为诸如0.8
之类的值有助于减少抖动图像中的噪点。默认值为1.0
。
用法
创建选项映射
{{ $opts := dict
"colors" (slice "222222" "808080" "dddddd")
"method" "ClusteredDot4x4"
"strength" 0.85
}}
创建滤镜
{{ $filter := images.Dither $opts }}
或使用默认设置创建滤镜
{{ $filter := images.Dither }}
使用 images.Filter
函数应用滤镜
{{ with resources.Get "images/original.jpg" }}
{{ with . | images.Filter $filter }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}
{{ end }}
您还可以使用 Resource
对象上的 Filter
方法应用滤镜
{{ with resources.Get "images/original.jpg" }}
{{ with .Filter $filter }}
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
{{ end }}
{{ end }}
抖动方法
请参阅Go 文档,了解以下每种抖动方法的描述。
误差扩散抖动方法
- Atkinson
- Burkes
- FalseFloydSteinberg
- FloydSteinberg
- JarvisJudiceNinke
- Sierra
- Sierra2
- Sierra2_4A
- Sierra3
- SierraLite
- Simple2D
- StevenPigeon
- Stucki
- TwoRowSierra
有序抖动方法
- ClusteredDot4x4
- ClusteredDot6x6
- ClusteredDot6x6_2
- ClusteredDot6x6_3
- ClusteredDot8x8
- ClusteredDotDiagonal16x16
- ClusteredDotDiagonal6x6
- ClusteredDotDiagonal8x8
- ClusteredDotDiagonal8x8_2
- ClusteredDotDiagonal8x8_3
- ClusteredDotHorizontalLine
- ClusteredDotSpiral5x5
- ClusteredDotVerticalLine
- Horizontal3x5
- Vertical5x3
示例
此示例使用默认的抖动选项。
原始

已处理

建议
无论使用哪种抖动方法,都要执行以下两项操作以获得最佳结果
- 在抖动之前缩放图像
- 将图像输出为无损格式,例如 GIF 或 PNG
下面的示例同时执行这两项操作,并将抖动调色板设置为图像中最主要的三个颜色。
{{ 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 }}
为了获得最佳效果,如果抖动调色板是灰度图,请在抖动之前将图像转换为灰度图。
{{ $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 }}
上面的示例
- 将图像大小调整为 800 像素宽
- 将图像转换为灰度图
- 使用带有灰度调色板的默认 (
FloydSteinberg
) 抖动方法对图像进行抖动 - 将图像转换为 PNG 格式