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

Extract dominant colors from image

extract_dominant_colors.livemd

Extract dominant colors from image

Mix.install([
  {:kino, "~> 0.11"},
  {:dominant_colors, "~> 0.1"}
])

Extract colors

path = "/path/to/image.ext"
image = File.read!(path)
image_type = :jpg
Kino.Image.new(content, image_type)
{:ok, colors} = DominantColors.dominant_colors(path)

styles = fn color ->
  [
    {"justify-content", "center"},
    {"align-items", "center"},
    {"color", "#ffffff"},
    {"display", "flex"},
    {"width", "250px"},
    {"height", "25px"},
    {"background-color", color},
    {"margin", "1px"}
  ]
  |> Enum.map(fn {k, v} -> k <> ": " <> v <> ";" end)
  |> Enum.join(" ")
end

html =
  Enum.map(colors, fn color ->
    "#{color}"
  end)
  |> Enum.join()

Kino.HTML.new(html)