Image Class
Pure-Rust image manipulation for resizing, cropping, transforms, filters, and format conversion. No system dependencies required.
Supported Formats
All methods return a new Image instance, enabling fluent chaining.
Loading Images
Image.new(path)
Loads an image from a file path. The format is detected automatically.
Parameters
path : String - Path to the image fileReturns
Image instance
let img = Image.new("photo.jpg")
println(img.width) # 1920
println(img.height) # 1080
Image.from_buffer(base64_string)
Loads an image from a base64-encoded string. Useful for processing images from HTTP requests, S3, or databases.
Parameters
base64_string : String - Base64-encoded image dataReturns
Image instance
let img = Image.from_buffer(base64_data)
println(img.width)
Properties
img.width / img.height
Returns the image dimensions in pixels.
Returns
Int
let img = Image.new("photo.jpg")
println("Size: " + str(img.width) + "x" + str(img.height))
Resizing
img.resize(width, height)
Resizes the image to the specified dimensions using high-quality Lanczos3 filtering.
Parameters
width : Int - Target width in pixelsheight : Int - Target height in pixelsReturns
New Image instance
let resized = Image.new("photo.jpg").resize(800, 600)
resized.to_file("photo_resized.jpg")
img.thumbnail(max_size)
Creates a thumbnail that fits within a square of the given size, preserving the original aspect ratio.
Parameters
max_size : Int - Maximum width or height in pixelsReturns
New Image instance
let thumb = Image.new("photo.jpg").thumbnail(200)
thumb.to_file("thumb.jpg")
img.crop(x, y, width, height)
Crops a rectangular region from the image. Coordinates must be non-negative.
Parameters
x : Int - Left offset (>= 0)y : Int - Top offset (>= 0)width : Int - Crop widthheight : Int - Crop heightReturns
New Image instance
let cropped = Image.new("photo.jpg").crop(100, 50, 400, 300)
cropped.to_file("cropped.jpg")
Transforms
img.grayscale()
Convert to grayscale.
img.invert()
Invert all colors.
img.flip_horizontal()
Flip horizontally (mirror).
img.flip_vertical()
Flip vertically.
img.rotate90()
Rotate 90° clockwise.
img.rotate180()
Rotate 180°.
img.rotate270()
Rotate 270° clockwise (90° counter-clockwise).
All transform methods return a new Image and can be chained:
let img = Image.new("photo.jpg")
.grayscale()
.flip_horizontal()
.rotate90()
img.to_file("transformed.jpg")
Adjustments
img.blur(sigma)
Applies a Gaussian blur. Higher sigma = more blur.
Parameters
sigma : Float or Int - Blur intensitylet blurred = Image.new("photo.jpg").blur(3.5)
img.brightness(value)
Adjusts image brightness. Positive values brighten, negative values darken.
Parameters
value : Int - Brightness adjustmentimg.contrast(value)
Adjusts image contrast. Positive increases contrast, negative decreases.
Parameters
value : Float or Int - Contrast adjustmentimg.hue_rotate(degrees)
Rotates the hue of all pixels by the given number of degrees.
Parameters
degrees : Int - Hue rotation in degreeslet adjusted = Image.new("photo.jpg")
.brightness(20)
.contrast(1.5)
.hue_rotate(90)
adjusted.to_file("adjusted.jpg")
Output Settings
img.quality(n)
Sets the output quality for JPEG encoding. Default is 85.
Parameters
n : Int - Quality from 1 (smallest) to 100 (best)img.format(fmt)
Sets the output format explicitly.
Parameters
fmt : String - One of: "jpeg", "png", "gif", "bmp", "ico", "tiff", "webp"# Convert PNG to JPEG at 70% quality
let img = Image.new("photo.png")
.format("jpeg")
.quality(70)
img.to_file("photo.jpg")
Saving & Exporting
img.to_file(path)
Saves the image to a file. Format is determined by: .format() setting, then file extension, then PNG fallback.
Parameters
path : String - Output file pathReturns
Boolean - true on success
Image.new("photo.jpg").thumbnail(200).to_file("thumb.jpg")
img.to_buffer()
Encodes the image to a base64 string. Useful for storing in databases, HTTP responses, or passing to S3.
Returns
String - Base64-encoded image data
let img = Image.new("photo.jpg").thumbnail(100)
let base64_data = img.to_buffer()
# Store in S3
S3.put_object("my-bucket", "thumb.jpg", base64_data, {
"content_type": "image/jpeg"
})
Complete Examples
Thumbnail generation in a controller
fn upload(req)
let file = req.files["avatar"]
let img = Image.from_buffer(file.data)
# Create multiple sizes
let large = img.resize(800, 800)
let medium = img.thumbnail(400)
let small = img.thumbnail(100)
large.to_file("public/uploads/avatar_large.jpg")
medium.to_file("public/uploads/avatar_medium.jpg")
small.to_file("public/uploads/avatar_small.jpg")
return { "status": 200, "body": "Upload complete" }
end
Image processing pipeline
let img = Image.new("raw_photo.jpg")
.resize(1200, 900)
.brightness(10)
.contrast(1.2)
.quality(85)
img.to_file("processed.jpg")
# Also create a grayscale thumbnail
img.grayscale().thumbnail(200).to_file("thumb_gray.jpg")
Format conversion with S3
# Convert all PNGs to WebP
let files = S3.list_objects("images", "photos/")
for file in files
if file.ends_with(".png")
let data = S3.get_object("images", file)
let img = Image.from_buffer(data)
.format("webp")
.quality(80)
let new_key = file.replace(".png", ".webp")
S3.put_object("images", new_key, img.to_buffer(), {
"content_type": "image/webp"
})
end
end