Imageflow.Graph (imageflow v0.4.1) View Source

With the Graph API, you can specify a task to process multiple inputs/outputs according to a pipeline of operations.

For example:

alias Imageflow.Graph

Graph.new()
|> Graph.decode_file("input.png")     # read input.png
|> Graph.constrain(200, 200)          # constrain image to 200x200
|> Graph.saturation(0.5)              # set saturation to 0.5 (-1..1 range)
|> Graph.encode_to_file("output.png") # specify output file
|> Graph.run()                        # run the job ```

Within a Graph, you specify all the operations, inputs and outputs you need. Only when Graph.run/1 is called is this actually executed

You can also specify multiple outputs, which is a common use-case. For example, for web/mobile development, you may want to generate multiple images with different resolutions for responsive apps:

alias Imageflow.Graph

Graph.new()
|> Graph.decode_file(@input_path)
|> Graph.branch(fn graph ->
  # 2160px wide image for retina displays graph
  graph
  |> Graph.constrain(2160, nil)
  |> Graph.encode_to_file("desktop@2x.png")
end)
|> Graph.branch(fn graph ->   # 1080px wide image for desktop
  graph
  |> Graph.constrain(1080, nil)
  |> Graph.encode_to_file("desktop.png")
end)
|> Graph.branch(fn graph ->   # 720px wide image for tablet
  graph
  |> Graph.constrain(720, nil)
  |> Graph.encode_to_file("tablet.png")
end)
|> Graph.branch(fn graph ->   # 600px wide image for mobile
  graph
  |> Graph.constrain(600, nil)
  |> Graph.encode_to_file("mobile.png")
end)
|> Graph.run()

The above snippet will generate 4 images, all processed under the same imageflow job.

Check the documentation to see what other operations are available

You may also want to check the base repo for imageflow, or its live JSON API documentation

Link to this section Summary

Functions

Creates a new branch, allowing you do add multiple paths and outputs to the graph

Adjusts brightness betweeen -1 and 1

Applies a color filter to the image. All filters will operate on the sRGB color space, so may not provide ideal results.

Constrains image dimensions. By default, it uses the "within" constraint mode from imageflow, but that can be overriden

Adjusts contrast betweeen -1 and 1

Appends a new input file to be decoded

Specifies a destination file for the current branch of the pipeline

Resizes the canvas according to the given amounts for each direction, filling in the empty space with the given HEX color

Draws a of the given color int he given coordinates

Flips the image horizontally

Flips the image vertically

Creates a new graph instance

Like a crop command, but you can specify coordinates outside of the image and thereby add padding. It's like a window.

Rotates image 180 degrees

Rotates image 270 degrees

Rotates image 90 degrees

Adjusts saturation betweeen -1 and 1

Adjusts the alpha channel of the image, from 0 (transparent) to 1 (opaque)

Transposes the image

Link to this section Types

Specs

point_t() :: {number(), number()}

Specs

t() :: %Imageflow.Graph{
  edges: term(),
  inputs: map(),
  io_count: integer(),
  node_count: integer(),
  nodes: map(),
  outputs: map(),
  tip: integer()
}

Link to this section Functions

Specs

branch(t(), (t() -> t())) :: t()

Creates a new branch, allowing you do add multiple paths and outputs to the graph

If you need to output multiple formats, this is what you should use. Perform all common operations first, then call a branch with each specialized part and its corresponding output

Link to this function

brightness(graph, amount)

View Source

Specs

brightness(t(), number()) :: t()

Adjusts brightness betweeen -1 and 1

Link to this function

color_filter(graph, filter)

View Source

Specs

color_filter(t(), number()) :: t()

Applies a color filter to the image. All filters will operate on the sRGB color space, so may not provide ideal results.

Check the JSON API documentation for available filters.

Link to this function

constrain(graph, w, h, opts \\ %{})

View Source

Specs

constrain(t(), number() | nil, number() | nil, map()) :: t()

Constrains image dimensions. By default, it uses the "within" constraint mode from imageflow, but that can be overriden:

Graph.constrain(graph, 800, 600, %{mode: "fit", gravity: %{percentage:
%{x: 50, y: 50}}

Check the JSON API docs for the constrain operation for more info on all options available

Specs

contrast(t(), number()) :: t()

Adjusts contrast betweeen -1 and 1

Link to this function

crop_whitespace(graph, threshold, percent_padding)

View Source

Specs

crop_whitespace(t(), number(), number()) :: t()
Link to this function

decode_file(graph, path)

View Source

Specs

decode_file(t(), binary()) :: t()

Appends a new input file to be decoded

Link to this function

encode_to_file(graph, path, encoder \\ :png, opts \\ %{})

View Source

Specs

encode_to_file(t(), binary(), binary() | atom(), map()) :: t()

Specifies a destination file for the current branch of the pipeline

No further processing operations should be appended at the current branch after this call.

The last two arguments specify the encoder and optional encoding parameters.

The following parameters are valid encoders:

  • :jpg: Alias to :mozjpeg
  • :jpeg: Alias to :mozjpeg
  • :png: Alias to :lodepng
  • :webp: Alias to:webplossless
  • :mozjpeg
  • :gif
  • :lodepng: Lossless PNG
  • :pngquant: Lossy PNG
  • :webplossy: Lossy WebP
  • :webplossless: Lossless WebP

Check the official encoding documentation to see the parameters available to each encoder

Link to this function

expand_canvas(graph, left, top, right, bottom, color_hex \\ "FFFFFF00")

View Source

Specs

expand_canvas(t(), number(), number(), number(), number(), binary()) :: t()

Resizes the canvas according to the given amounts for each direction, filling in the empty space with the given HEX color

Link to this function

fill_rect(graph, x1, y1, x2, y2, color \\ "black")

View Source

Specs

fill_rect(t(), number(), number(), number(), number(), binary()) :: t()

Draws a of the given color int he given coordinates

Specs

flip_horizontal(t()) :: t()

Flips the image horizontally

Specs

flip_vertical(t()) :: t()

Flips the image vertically

Specs

new() :: t()

Creates a new graph instance

Link to this function

region(graph, x1, y1, x2, y2, bg_color \\ "transparent")

View Source

Specs

region(t(), number(), number(), number(), number(), binary()) :: t()

Like a crop command, but you can specify coordinates outside of the image and thereby add padding. It's like a window.

Link to this function

region_percent(graph, x1, y1, x2, y2, bg_color \\ "transparent")

View Source

Specs

region_percent(t(), number(), number(), number(), number(), binary()) :: t()

Specs

rotate_180(t()) :: t()

Rotates image 180 degrees

Specs

rotate_270(t()) :: t()

Rotates image 270 degrees

Specs

rotate_90(t()) :: t()

Rotates image 90 degrees

Link to this function

saturation(graph, amount)

View Source

Specs

saturation(t(), number()) :: t()

Adjusts saturation betweeen -1 and 1

Link to this function

transparency(graph, opacity)

View Source

Specs

transparency(t(), number()) :: t()

Adjusts the alpha channel of the image, from 0 (transparent) to 1 (opaque)

Specs

transpose(t()) :: t()

Transposes the image