image
import "std/data" (included in import "std") · namespace std.data
Raster images: PNG and JPEG read and written, resized, cropped, rotated and converted, thumbnails and avatars made, and pixels read and set.
grav
import "std"
fn main() {
let img = Image.create(400, 300, 3, 0xffffffff)
img.fillRect(0, 0, 200, 300, 0x1e88e5ff)
img.set(399, 299, 0xff0000ff)
print("${img.width}x${img.height}, ${img.channels} channels, red corner: ${img.get(399, 299) == 0xff0000ff}")
let thumb = img.thumbnail(100, 100)
print("thumbnail ${thumb.width}x${thumb.height}")
let avatar = img.cover(64, 64)
print("avatar ${avatar.width}x${avatar.height}")
print("rotated ${img.rotate(90).width}x${img.rotate(90).height}")
let png = thumb.png()
let back = Image.decode(png)
print("${Image.sniff(png)}: ${back.width}x${back.height}, same pixels ${back.pixels() == thumb.pixels()}")
print("grey ${img.toGray().channels}, with alpha ${img.toRgba().channels}")
}400x300, 3 channels, red corner: true
thumbnail 100x75
avatar 64x64
rotated 300x400
png: 100x75, same pixels true
grey 1, with alpha 4- Pixels are 8 bits per channel: 1 (grey), 2 (grey + alpha), 3 (RGB) or 4 (RGBA) channels, rows top to bottom.
getandsettake colours as0xRRGGBBAA.pixels()andImage.fromPixelsmove the raw bytes in and out. - Reading:
Image.decode(bytes)(the format is sniffed) andImage.read(path). PNG reading covers every colour type, every bit depth (1 to 16; 16-bit becomes 8), palettes with transparency, and Adam7 interlacing. JPEGs are turned upright by their EXIF orientation. - Writing:
png(level)uses the row filter that compresses each row best.jpeg(quality)flattens transparency onto white and keeps full colour resolution from quality 90 up.write(path)picks the format by extension. - Geometry:
resize(w, h, filter)(Resample.Lanczosby default;Bicubic,Bilinear,Nearest),resizeToWidth,resizeToHeight,fit(inside a box),thumbnail(fit, but never enlarges),cover(fill a box, centre-cropped),crop,rotate(90 | 180 | 270),flipHorizontal,flipVertical,oriented(exif). Conversions:toGray,toRgb(background),toRgba,convert(channels). Drawing:fill,fillRect,draw(other, x, y)(blended by alpha). - Speed: resizing is separable and works on premultiplied alpha, so transparent edges get no dark fringe. When shrinking by more than 4×, a box average runs first. A 12-megapixel image becomes a thumbnail in about 60 ms, and PNG encoding runs about as fast as Pillow's.
- Libraries: PNG uses the system zlib, as
Compressdoes. JPEG uses libturbojpeg, loaded the first time it is needed: on Debian and Ubuntu the package islibturbojpeg0, on macOSbrew install jpeg-turbo.Image.jpegAvailable()tells whether it was found.
API
enum Resample
How resize samples: Lanczos-3 (sharpest; the default), bicubic, bilinear or nearest (blocky; for pixel art and masks).
Values
Resample.NearestResample.BilinearResample.BicubicResample.Lanczos
class Image
An 8-bit raster image.
Constructors
new Image(width: int, height: int, channels: int)
Static members
static fn create(width: int, height: int, channels: int = 4, color: long = 0x000000ff) -> Image— Awidthxheightimage filled withcolor(0xRRGGBBAA).static fn decode(data: Bytes) -> Image— A PNG or JPEG decoded (the format is sniffed); JPEGs are turned upright by their EXIF orientation. ValueError for anything else or damaged data.static fn read(path: string) -> Image— decode, from a file.static fn sniff(data: Bytes) -> string— "png", "jpeg" or "" for data that is neither.static fn jpegAvailable() -> bool— Whether JPEG support (libturbojpeg) could be loaded.static fn fromPixels(data: Bytes, width: int, height: int, channels: int) -> Image— An image from raw pixel bytes.
Fields
width: intheight: intchannels: int— 1 grey, 2 grey + alpha, 3 RGB, 4 RGBA.
Methods
fn png(level: int = 6) -> Bytes— The image as a PNG (level0 to 9: zlib's effort; 6 is the usual balance).fn jpeg(quality: int = 85) -> Bytes— The image as a JPEG (quality1 to 100). Transparency is flattened onto white; colour is subsampled 4:2:0 below quality 90 and kept full (4:4:4) from 90.fn write(path: string, quality: int = 85)— Writes a PNG or JPEG, by the path's extension (.png, .jpg, .jpeg).fn get(x: int, y: int) -> long— The pixel at (x, y) as 0xRRGGBBAA (grey repeats into R, G and B; no alpha is ff).fn set(x: int, y: int, color: long)— Sets the pixel at (x, y) from 0xRRGGBBAA (grey images take the luma).fn fill(color: long)— Every pixel set tocolor.fn fillRect(x: int, y: int, w: int, h: int, color: long)— A rectangle set tocolor(clipped to the image).fn draw(other: Image, x: int, y: int)—otherdrawn over this image with its top-left corner at (x, y), blended by its alpha (a watermark, a logo, a QR code).fn pixels() -> Bytes— The raw pixel bytes (rows top to bottom,channelsbytes a pixel).fn copy() -> Image— A copy.fn crop(x: int, y: int, w: int, h: int) -> Image— Thewxhrectangle at (x, y) (clipped to the image).fn resize(w: int, h: int, filter: Resample = Resample.Lanczos) -> Image— Scaled to exactlywxh.fn resizeToWidth(w: int) -> Image— Scaled towwide, the height following the aspect ratio.fn resizeToHeight(h: int) -> Image— Scaled tohhigh, the width following the aspect ratio.fn thumbnail(maxW: int, maxH: int) -> Image— Scaled down (never up) to fit inmaxWxmaxH, aspect kept.fn fit(w: int, h: int) -> Image— Scaled (up or down) to fit inwxh, aspect kept.fn cover(w: int, h: int) -> Image— Scaled to fillwxh, aspect kept, and the overflow cropped evenly (an avatar).fn flipHorizontal() -> Imagefn flipVertical() -> Imagefn rotate(degrees: int) -> Image— Turned 90, 180 or 270 degrees clockwise.fn oriented(orientation: int) -> Image— Turned upright from an EXIF orientation (1 to 8).fn toGray() -> Image— Converted to grey (Rec. 601 luma; transparency flattened onto white).fn toRgb(background: long = 0xffffffff) -> Image— Converted to RGB (transparency flattened ontobackground, 0xRRGGBBAA).fn toRgba() -> Image— Converted to RGBA.fn convert(channels: int) -> Image— Converted tochannelschannels.