Add metrics around image resize/convert/dominant color extration
This commit is contained in:
@@ -14,8 +14,15 @@ defmodule MusicLibrary.Assets.Image do
|
||||
|
||||
@spec resize(binary(), pos_integer(), String.t()) :: {:ok, binary()} | {:error, term()}
|
||||
def resize(cover_data, size \\ @default_size, format \\ @default_format) do
|
||||
{:ok, thumb} = Operation.thumbnail_buffer(cover_data, size)
|
||||
Image.write_to_buffer(thumb, extension(format))
|
||||
:telemetry.span(
|
||||
[:music_library, :assets, :image, :resize],
|
||||
%{},
|
||||
fn ->
|
||||
{:ok, thumb} = Operation.thumbnail_buffer(cover_data, size)
|
||||
result = Image.write_to_buffer(thumb, extension(format))
|
||||
{result, %{}}
|
||||
end
|
||||
)
|
||||
end
|
||||
|
||||
@spec convert(binary(), String.t(), String.t()) :: {:ok, binary()}
|
||||
@@ -23,8 +30,15 @@ defmodule MusicLibrary.Assets.Image do
|
||||
if data_format == target_format do
|
||||
{:ok, cover_data}
|
||||
else
|
||||
{:ok, image} = Image.new_from_buffer(cover_data)
|
||||
Image.write_to_buffer(image, extension(target_format))
|
||||
:telemetry.span(
|
||||
[:music_library, :assets, :image, :convert],
|
||||
%{},
|
||||
fn ->
|
||||
{:ok, image} = Image.new_from_buffer(cover_data)
|
||||
result = Image.write_to_buffer(image, extension(target_format))
|
||||
{result, %{}}
|
||||
end
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -7,12 +7,21 @@ defmodule MusicLibrary.Colors.KMeansExtractor do
|
||||
|
||||
@spec extract_dominant_colors(binary(), pos_integer()) :: {:ok, [String.t()]} | {:error, term()}
|
||||
def extract_dominant_colors(image_data, num_colors \\ 5) do
|
||||
with {:ok, dir} <- Briefly.create(type: :directory),
|
||||
path = dir <> "/temp_image.jpg",
|
||||
{:ok, image} <- Image.new_from_buffer(image_data),
|
||||
:ok <- Image.write_to_file(image, path),
|
||||
{:ok, colors} <- DominantColors.dominant_colors(path) do
|
||||
{:ok, Enum.take(colors, num_colors)}
|
||||
end
|
||||
:telemetry.span(
|
||||
[:music_library, :colors, :extract],
|
||||
%{},
|
||||
fn ->
|
||||
result =
|
||||
with {:ok, dir} <- Briefly.create(type: :directory),
|
||||
path = dir <> "/temp_image.jpg",
|
||||
{:ok, image} <- Image.new_from_buffer(image_data),
|
||||
:ok <- Image.write_to_file(image, path),
|
||||
{:ok, colors} <- DominantColors.dominant_colors(path) do
|
||||
{:ok, Enum.take(colors, num_colors)}
|
||||
end
|
||||
|
||||
{result, %{}}
|
||||
end
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user