Elixir NIF to read and write image data from/to PNG and JPEG files.
For PNG images, it uses the lodepng C library; for JPEG images, it uses the ujpeg C library.
Progressive or lossless JPEG files, as well as JPEG encoding are not supported by the underlying uJpeg.
Pixels.read
and Pixels.read_file
allow you to decode a PNG / JPEG file into
a %Pixels{}
struct. The data
component of this struct contains the RGBA data
of the image, in row order. To get an individual pixel's value, you can
calculate an offset into this data based on the x/y coordinate. For example:
{:ok, pixels} = Pixels.read_file("test.png")
# the pixel value you want to get:
x = 10
y = 3
# calculate offset into `pixels.data`
offset = (y * pixels.width + x) * 4
# binary-pattern-match the pixel value in the data:
<<_::binary-size(offset), r, g, b, alpha, _::binary>> = pixels.data
Pull requests welcome! For a more feature-complete library you could also use image, however its license is LGPL so a bit more restrictive than the MIT license of Pixels.
If available in Hex, the package can be installed
by adding pixels
to your list of dependencies in mix.exs
:
def deps do
[
{:pixels, "~> 0.1.0"}
]
end
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/pixels.