A library for generating PNG images.
If available in Hex, the package can be installed
by adding pngex to your list of dependencies in
mix.exs
:
def deps do
[
{:pngex, "~> 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/pngex.
Use binary, list of integers or list of tuples to express pixel data of the image.
type | example |
---|---|
:rgb |
<<r0, g0, b0, r1, g1, b1, ...>> |
:rgba |
<<r0, g0, b0, a0, r1, g1, b1, a1, ...>> |
:gray |
<<c0, c1, ...> |
:gray_and_alpha |
<<c0, a0, c1, a1, ...>> |
:indexed |
<<c0, c1, ...> |
type | example |
---|---|
:rgb |
[r0, g0, b0, r1, g1, b1, ...] |
:rgba |
[r0, g0, b0, a0, r1, g1, b1, a1, ...] |
:gray |
[c0, c1, ...] |
:gray_and_alpha |
[c0, a0, c1, a1, ...] |
:indexed |
[c0, c1, ...] |
type | example |
---|---|
:rgb |
[{r0, g0, b0}, {r1, g1, b1}, ...] |
:rgba |
[{r0, g0, b0, a0}, {r1, g1, b1, a1}, ...] |
bitmap =
for n <- 0..65535 do
r = div(n, 256)
g = rem(n, 256)
b = 255 - max(r, g)
{r, g, b}
end
pngex =
Pngex.new()
|> Pngex.set_type(:rgb)
|> Pngex.set_depth(:depth8)
|> Pngex.set_size(256, 256)
image = Pngex.generate(pngex, bitmap)
File.write("rgb8_256x256.png", image)
or
image =
Pngex.new(
type: :rgb,
depth: :depth8,
width: 256,
height: 256
)
|> Pngex.generate(bitmap)
File.write("rgb8_256x256.png", image)
palette = for n <- 0..255, do: {n, 255, 255 - n}
bitmap = for n <- 0..65535, do: rem(n, 256)
image =
Pngex.new(
type: :indexed,
depth: :depth8,
width: 256,
height: 256,
palette: palette
)
|> Pngex.generate(bitmap)
File.write("indexed8_256x256.png", image)
bitmap = for n <- 0..65535, do: rem(n, 256)
image =
Pngex.new(
type: :gray,
depth: :depth8,
width: 256,
height: 256
)
|> Pngex.generate(bitmap)
File.write("gray8_256x256.png", image)
color type \ depth | :depth1 |
:depth2 |
:depth4 |
:depth8 |
:depth16 |
---|---|---|---|---|---|
:indexed |
✓ | ✓ | ✓ | ✓ | |
:gray |
✓ | ✓ | ✓ | ✓ | ✓ |
:gray_and_alpha |
✓ | ✓ | |||
:rgb |
✓ | ✓ | |||
:rgba |
✓ | ✓ |