images.Dither

New in v0.123.0

選項

colors
(string array) 一組顏色,構成抖動調色板,每個顏色應該是 RGB 或 RGBA 的 十六進制 值,無論是否有前置的 # 符號。預設值是不透明的黑色 (000000ff) 和不透明的白色 (ffffffff)。
method
(string) 抖動方法。請參見下方的 抖動方法 節來查看可用方法的列表。預設為 FloydSteinberg
serpentine
(bool) 只適用於誤差擴散抖動方法,該選項控制誤差擴散矩陣是否按蛇形方式應用,即每隔一行變為從右向左進行。這樣可以大大減少直線型伪影。預設值是 true
strength
(float) 應用抖動矩陣的強度,通常在範圍 [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

範例

此範例使用預設的抖動選項。

Original

錫安國家公園

Processed

錫安國家公園

建議

無論使用哪種抖動方法,為了獲得最佳效果,請執行以下兩個操作:

  1. 在進行抖動之前調整圖像大小
  2. 將圖像輸出為無損格式,如 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 }}

上面的範例:

  1. 將圖像調整為 800 像素寬
  2. 將圖像轉換為灰階
  3. 使用預設的 FloydSteinberg 抖動方法,並使用灰階調色板進行抖動
  4. 將圖像轉換為 PNG 格式