Last active
December 27, 2023 09:45
-
-
Save 29decibel/845ced62bfbaa25cd5ed064e0bf3b6cf to your computer and use it in GitHub Desktop.
Extract donimant colors using dominant_colors in Elixir Livebook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Extract dominant colors from image | |
```elixir | |
Mix.install([ | |
{:kino, "~> 0.11.3"}, | |
{:dominant_colors, "~> 0.1.4"} | |
]) | |
``` | |
## Extract colors | |
```elixir | |
image_path = "/Users/mikeli/Downloads/111ebfdc244345c3.png" | |
content = File.read!(image_path) | |
Kino.Image.new(content, :png) | |
``` | |
```elixir | |
{:ok, colors} = DominantColors.dominant_colors(image_path) | |
styles = fn color -> | |
[ | |
{"justify-content", "center"}, | |
{"align-items", "center"}, | |
{"color", "white"}, | |
{"display", "inline-flex"}, | |
{"width", "100px"}, | |
{"height", "30px"}, | |
{"background-color", color}, | |
{"margin", "1px"} | |
] | |
|> Enum.map(fn {k, v} -> k <> ": " <> v <> ";" end) | |
|> Enum.join(" ") | |
end | |
html = | |
Enum.map(colors, fn color -> | |
"<div style=\"#{styles.(color)}\">#{color}</div>" | |
end) | |
|> Enum.join() | |
Kino.HTML.new(html) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment