load_image
result = load_image(path)
Input: path · Output: result
What it is
path is a string file path. Reads the file from disk and decodes it — PNG, BMP, JPEG and TIFF. The format is sniffed from the file's first bytes rather than its extension, because a .png that is really a BMP is a thing that happens and the bytes are never wrong. PNG: bit depths 1/2/4/8/16, colour types grey, truecolour, palette, grey+alpha and RGBA; alpha composites onto white, since Image is three channels. Adam7 interlacing is refused by name rather than decoded wrongly. JPEG: baseline and progressive, grayscale (widened to R=G=B) and colour; 16-bit and CMYK JPEGs are refused by name rather than converted, because guessing CMYK's Adobe inversion convention wrong yields silently colour-inverted pixels. TIFF: 8- and 16-bit integer samples in grayscale, grayscale+alpha, RGB and RGBA (16-bit is scaled down to 8, since Image is 8-bit; alpha composites onto white as with PNG); floating-point, palette, CMYK and YCbCr TIFFs are refused by name.
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = load_image(path) | path string or Imagenot documented yet | result Image | Returns a new Image sized to the file's own dimensions. JPEG and TIFF reading added in v0.3.0. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
img = load_image("sample.bmp")
rotated = imrotate(img, 45, method="bilinear", bbox="loose")
imshow(rotated)
from images
img = load_image("sample.bmp")
edges = edge_detect(grayscale(img))
imshow(edges)
from images
img = load_image("sample.bmp")
# Rotate 30 degrees about the origin, then shift right/down by (50, 20).
M = affine_translate(50, 20) * affine_rotate(30)
out = imwarp(img, M, method="bilinear", bbox="loose", fill=255)
imshow(out)
from images
# A gradient with a bright bar in it, so the rotation below is visible:
# a symmetric test image would look identical afterwards.
n = 48
g = zeros(n, n)
for i in 0 to n - 1
for j in 0 to n - 1
g[i, j] = 40 + 3 * j
if i > 10 and i < 22 and j > 6 and j < 30
g[i, j] = 240
end if
end for
end for
save_image("sample.bmp", image_from_matrix(g))
img = load_image("sample.bmp")
gray = grayscale(img)
imshow(gray)
from images
How it works
load_image(path) — PNG, BMP, JPEG and TIFF (see the image module doc for the pixel/file-format decisions: PNG and BMP are hand-rolled there on top of the existing DEFLATE code, JPEG and "clear error on a bad path/file" convention as read_csv/write_csv.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.