Kreuzberg.Image (kreuzberg v4.0.8)

View Source

Structure representing an extracted image with optional OCR results.

Contains binary image data and metadata extracted from documents, along with OCR text results when OCR processing is enabled.

Fields

  • :data - Binary image data (PNG, JPEG, WebP, etc.)
  • :format - Image format as string ("png", "jpeg", "webp", etc.)
  • :width - Image width in pixels
  • :height - Image height in pixels
  • :mime_type - MIME type of the image (e.g., "image/png")
  • :ocr_text - Text extracted from image via OCR
  • :page_number - Page number where image appears
  • :file_size - Size of image data in bytes
  • :dpi - Dots per inch of the image

Examples

iex> image = %Kreuzberg.Image{
...>   format: "png",
...>   width: 800,
...>   height: 600,
...>   data: <<...>>,
...>   ocr_text: "Extracted text from image"
...> }
iex> image.format
"png"

Summary

Functions

Returns the aspect ratio (width / height) of the image.

Creates an Image struct from a map.

Returns whether the image has binary data.

Creates a new Image struct with required format field.

Converts an Image struct to a map.

Types

t()

@type t() :: %Kreuzberg.Image{
  data: binary() | nil,
  dpi: integer() | nil,
  file_size: integer() | nil,
  format: String.t() | nil,
  height: integer() | nil,
  mime_type: String.t() | nil,
  ocr_text: String.t() | nil,
  page_number: integer() | nil,
  width: integer() | nil
}

Functions

aspect_ratio(arg1)

@spec aspect_ratio(t()) :: float() | nil

Returns the aspect ratio (width / height) of the image.

Parameters

  • image - An Image struct

Returns

The aspect ratio as a float, or nil if dimensions not available.

Examples

iex> image = %Kreuzberg.Image{width: 1920, height: 1080}
iex> Kreuzberg.Image.aspect_ratio(image)
1.7777777777777777

iex> image = %Kreuzberg.Image{format: "png"}
iex> Kreuzberg.Image.aspect_ratio(image)
nil

from_map(data)

@spec from_map(map()) :: t()

Creates an Image struct from a map.

Converts a plain map (typically from NIF/Rust) into a proper struct.

Parameters

  • data - A map containing image fields

Returns

An Image struct with matching fields populated.

Examples

iex> image_map = %{
...>   "format" => "png",
...>   "width" => 1024,
...>   "height" => 768
...> }
iex> Kreuzberg.Image.from_map(image_map)
%Kreuzberg.Image{
  format: "png",
  width: 1024,
  height: 768
}

has_data?(image)

@spec has_data?(t()) :: boolean()

Returns whether the image has binary data.

Parameters

  • image - An Image struct

Returns

true if image has data, false otherwise.

Examples

iex> image = %Kreuzberg.Image{format: "png", data: <<1, 2, 3>>}
iex> Kreuzberg.Image.has_data?(image)
true

iex> image = %Kreuzberg.Image{format: "png"}
iex> Kreuzberg.Image.has_data?(image)
false

new(format, opts \\ [])

@spec new(
  String.t(),
  keyword()
) :: t()

Creates a new Image struct with required format field.

Parameters

  • format - The image format (e.g., "png", "jpeg")
  • opts - Optional keyword list with:
    • :data - Binary image data
    • :width - Image width in pixels
    • :height - Image height in pixels
    • :mime_type - MIME type
    • :ocr_text - OCR extracted text
    • :page_number - Page number
    • :file_size - File size in bytes
    • :dpi - DPI setting

Returns

An Image struct with the provided format and options.

Examples

iex> Kreuzberg.Image.new("png")
%Kreuzberg.Image{format: "png"}

iex> Kreuzberg.Image.new(
...>   "jpeg",
...>   width: 1920,
...>   height: 1080,
...>   dpi: 150
...> )
%Kreuzberg.Image{
  format: "jpeg",
  width: 1920,
  height: 1080,
  dpi: 150
}

to_map(image)

@spec to_map(t()) :: map()

Converts an Image struct to a map.

Useful for serialization and passing to external systems.

Parameters

  • image - An Image struct

Returns

A map with string keys representing all fields.

Examples

iex> image = %Kreuzberg.Image{format: "png", width: 800}
iex> Kreuzberg.Image.to_map(image)
%{
  "format" => "png",
  "width" => 800,
  "data" => nil,
  ...
}