Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Reduce colors

livebooks/vix/reduce_colors.livemd

Reduce colors

Mix.install(
  [
    {:image, "~> 0.54"},
    {:kino, "~> 0.14"},
    {:scholar, "~> 0.3"},
    {:exla, "~> 0.9"}
  ],
  config: [nx: [default_backend: EXLA.Backend]]
)

Prepare image

{puppies_img, _} =
  "/home/livebook/vix/puppies.png"
  |> Image.open!()
  |> Image.split_alpha()

puppies_img

Reduce colors

Image.k_means!(puppies_img)
Image.k_means!(puppies_img, num_clusters: 8)
|> Enum.map(fn color ->
  Image.new!(100, 100, color: color)
end)
|> Kino.Layout.grid(columns: 8)
Image.reduce_colors!(puppies_img)
[
  puppies_img,
  Image.reduce_colors!(puppies_img, colors: 10),
  Image.reduce_colors!(puppies_img, colors: 8),
  Image.reduce_colors!(puppies_img, colors: 6),
  Image.reduce_colors!(puppies_img, colors: 4),
  Image.reduce_colors!(puppies_img, colors: 2)
]
|> Kino.Layout.grid(columns: 3)