View Source ImageRs (image_rs v0.3.1)

Summary

Functions

Adjust the contrast of this image.

Performs a Gaussian blur on this image.

Brighten the pixels of this image.

Return a cut-out of this image delimited by the bounding rectangle.

Encode this image as format.

Filters this image with the specified 3x3 kernel.

Flip this image horizontally

Flip this image vertically

Decode image from buffer in memory

Similar to from_binary/1 but raises on errors

Decode image from a given file

Similar to from_file/1 but raises on errors

Creates an ImageRs from a Nx tensor.

Return a grayscale version of this image.

Hue rotate the supplied image.

Invert the colors of this image.

Create a new ImageRs from given binary with corresponding parameters.

Resize this image using the specified filter algorithm.

Resize this image using the specified filter algorithm.

Resize this image using the specified filter algorithm.

Rotate this image 90 degrees clockwise.

Rotate this image 180 degrees clockwise.

Rotate this image 270 degrees clockwise.

Saves the buffer to a file at the path specified.

Saves the buffer to a file at the path specified in the specified format.

Get binary representation of pixels.

Converts an ImageRs to a Nx tensor.

Performs an unsharpen mask on this image.

Types

@type output_format() ::
  :png
  | :jpeg
  | :pnm
  | :gif
  | :ico
  | :bmp
  | :farbfeld
  | :tga
  | :exr
  | :tiff
  | :avif
  | :qoi
  | :webp
@type t() :: %ImageRs{
  channels: non_neg_integer(),
  color_type: :l | :la | :rgb | :rgba | :unknown,
  dtype: :u8 | :u16 | :f32,
  height: non_neg_integer(),
  resource: reference(),
  shape: [non_neg_integer()],
  width: non_neg_integer()
}

Functions

Link to this function

adjust_contrast(image, contrast)

View Source
@spec adjust_contrast(t(), float()) :: {:ok, t()} | {:error, String.t()}

Adjust the contrast of this image.

contrast is the amount to adjust the contrast by.

Negative values decrease the contrast and positive values increase the contrast.

@spec blur(t(), float()) :: {:ok, t()} | {:error, String.t()}

Performs a Gaussian blur on this image.

sigma is a measure of how much to blur by.

@spec brighten(t(), integer()) :: {:ok, t()} | {:error, String.t()}

Brighten the pixels of this image.

value is the amount to brighten each pixel by.

Negative values decrease the brightness and positive values increase it.

Link to this function

crop(image, x, y, height, width)

View Source
@spec crop(
  t(),
  non_neg_integer(),
  non_neg_integer(),
  non_neg_integer(),
  non_neg_integer()
) :: {:ok, t()} | {:error, String.t()}

Return a cut-out of this image delimited by the bounding rectangle.

Link to this function

encode_as(image, format, options \\ [])

View Source
@spec encode_as(t(), output_format(), Keyword.t()) ::
  {:ok, binary()} | {:error, String.t()}

Encode this image as format.

Link to this function

filter3x3(image, kernel)

View Source
@spec filter3x3(
  t(),
  [number()]
) :: {:ok, t()} | {:error, String.t()}

Filters this image with the specified 3x3 kernel.

@spec fliph(t()) :: {:ok, t()} | {:error, String.t()}

Flip this image horizontally

@spec flipv(t()) :: {:ok, t()} | {:error, String.t()}

Flip this image vertically

@spec from_binary(binary()) :: {:ok, t()} | {:error, String.t()}

Decode image from buffer in memory

  • data. Image data in memory.

Example

# image buffer from a file or perhaps download from the Internet
{:ok, data} = File.read("/path/to/image")

# decode the image from memory
{:ok, image} = ImageRs.from_binary(data)
width = image.width
height = image.height
channels = image.channels
shape = image.shape
{^height, ^width, ^channels} = shape
color_type = image.color_type
type = image.type
@spec from_binary!(binary()) :: t()

Similar to from_binary/1 but raises on errors

@spec from_file(Path.t()) :: {:ok, t()} | {:error, String.t()}

Decode image from a given file

  • filename. Path to the image.

Example

{:ok, image} = ImageRs.from_file("/path/to/image")
width = image.width
height = image.height
channels = image.channels
shape = image.shape
{^height, ^width, ^channels} = shape
color_type = image.color_type
type = image.type
@spec from_file!(Path.t()) :: t()

Similar to from_file/1 but raises on errors

Creates an ImageRs from a Nx tensor.

The tensor is expected to have the shape {h, w, c} and one of the supported types (u8/u16/f32).

@spec grayscale(t()) :: {:ok, t()} | {:error, String.t()}

Return a grayscale version of this image.

Returns Luma images in most cases. However, for f32 images, this will return a grayscale Rgb/Rgba image instead.

@spec huerotate(t(), integer()) :: {:ok, t()} | {:error, String.t()}

Hue rotate the supplied image.

value is the degrees to rotate each pixel by.

0 and 360 do nothing, the rest rotates by the given degree value.

just like the css webkit filter hue-rotate(180)

@spec invert(t()) :: {:ok, t()} | {:error, String.t()}

Invert the colors of this image.

Link to this function

new(height, width, color_type, dtype, data)

View Source
@spec new(
  pos_integer(),
  pos_integer(),
  :l | :la | :rgb | :rgba,
  :u8 | :u16 | :f32,
  binary()
) ::
  {:ok, t()} | {:error, String.t()}

Create a new ImageRs from given binary with corresponding parameters.

Link to this function

resize(image, height, width, filter_type \\ :lanczos3)

View Source
@spec resize(
  t(),
  non_neg_integer(),
  non_neg_integer(),
  :nearest | :triangle | :catmull_rom | :gaussian | :lanczos3
) :: {:ok, t()} | {:error, String.t()}

Resize this image using the specified filter algorithm.

Returns a new image. Does not preserve aspect ratio.

height and width are the new image's dimensions.

Link to this function

resize_preserve_ratio(image, height, width, filter_type \\ :lanczos3)

View Source
@spec resize_preserve_ratio(
  t(),
  non_neg_integer(),
  non_neg_integer(),
  :nearest | :triangle | :catmull_rom | :gaussian | :lanczos3
) :: {:ok, t()} | {:error, String.t()}

Resize this image using the specified filter algorithm.

Returns a new image. The image's aspect ratio is preserved.

The image is scaled to the maximum possible size that fits within the bounds specified by width and height.

Link to this function

resize_to_fill(image, height, width, filter_type \\ :lanczos3)

View Source
@spec resize_to_fill(
  t(),
  non_neg_integer(),
  non_neg_integer(),
  :nearest | :triangle | :catmull_rom | :gaussian | :lanczos3
) :: {:ok, t()} | {:error, String.t()}

Resize this image using the specified filter algorithm.

Returns a new image. The image's aspect ratio is preserved.

The image is scaled to the maximum possible size that fits within the larger (relative to aspect ratio) of the bounds specified by height and width, then cropped to fit within the other bound.

@spec rotate90(t()) :: {:ok, t()} | {:error, String.t()}

Rotate this image 90 degrees clockwise.

@spec rotate180(t()) :: {:ok, t()} | {:error, String.t()}

Rotate this image 180 degrees clockwise.

@spec rotate270(t()) :: {:ok, t()} | {:error, String.t()}

Rotate this image 270 degrees clockwise.

@spec save(t(), Path.t()) :: :ok | {:error, String.t()}

Saves the buffer to a file at the path specified.

Link to this function

save_with_format(image, path, format)

View Source
@spec save_with_format(t(), Path.t(), output_format()) :: :ok | {:error, String.t()}

Saves the buffer to a file at the path specified in the specified format.

@spec to_binary(t()) :: {:ok, binary()} | {:error, String.t()}

Get binary representation of pixels.

Link to this function

to_nx(image, opts \\ [])

View Source

Converts an ImageRs to a Nx tensor.

It accepts the same options as Nx.from_binary/3.

Link to this function

unsharpen(image, sigma, threshold)

View Source
@spec unsharpen(t(), float(), integer()) :: {:ok, t()} | {:error, String.t()}

Performs an unsharpen mask on this image.

sigma is the amount to blur the image by.

threshold is a control of how much to sharpen.