How can I extract the top N dominant colors from an image using Elixir?

I’m working on a project in Elixir where I need to extract the top N dominant colors from an image. I’ve been searching for Elixir libraries or methods that can help me accomplish this task efficiently. However, I’m not sure which library or approach to choose, and I could use some guidance.

https://hexdocs.pm/image/Image.html#dominant_color/2

3 Likes

I implemented my requirement with this Image library.

Mix.install([
  {:image, "~> 0.37"},
  {:kino, "~> 0.10.0"},
  {:vix, "~> 0.5"}
])

Section

file = Kino.Input.file("File")
file_reading = Kino.Input.read(file)
file_path = Kino.Input.file_path(file_reading.file_ref)
{:ok, img} = Vix.Vips.Image.new_from_file(file_path)
color = Image.dominant_color(img, [{:bins, x}])
Image.Color.rgb_to_hex(color)
2 Likes

You might also consider:

  • Image.from_kino/2 which allows you to consume an image from Kino directly or
  • Image.open/2 which can open an image from the file system, or a stream.
3 Likes