tensors/imagekit

0.1.1
imagekit

A general-purpose image processing library for Ballerina — the Ballerina equivalent of Python's Pillow. Wraps Java's javax.imageio and java.awt via Ballerina–Java interop.

Supported formats: PNG, JPEG, BMP, GIF

Installation

Copy
[[dependency]]
org     = "vishwajayawickrama"
name    = "imagekit"
version = "0.1.0"

Quick Start

Copy
import tensors/imagekit;
import ballerina/io;

public function main() returns error? {
    // Read metadata
    imagekit:ImageInfo info = check imagekit:getInfo("photo.png");
    io:println(info.width.toString() + "x" + info.height.toString() + " " + info.format);

    // Crop 20px from each edge
    check imagekit:crop("photo.png", top = 20, bottom = 20, left = 10, right = 10);

    // Resize to exact dimensions
    check imagekit:resize("photo.png", 1920, 1080);

    // Resize preserving aspect ratio
    check imagekit:resizeToFit("photo.png", 800, 600);

    // Rotate 90° clockwise
    check imagekit:rotate("photo.png", 90);

    // Mirror
    check imagekit:flipHorizontal("photo.png");
    check imagekit:flipVertical("photo.png");

    // Grayscale
    check imagekit:toGrayscale("photo.png");

    // Convert format (writes a new file)
    check imagekit:convert("photo.png", "photo.jpg", "JPEG");

    // Create thumbnail fitting within 200×200, aspect ratio preserved
    check imagekit:thumbnail("photo.png", "thumb.png", 200, 200);
}

Batch Crop

Copy
imagekit:CropSummary summary = check imagekit:cropDirectory(
    "images/",
    top    = 20,
    bottom = 20,
    backup = true   // saves originals as *.orig.png
);
io:println("Cropped " + summary.processed.toString() + " files, "
    + summary.pixelReductionPct.toString() + "% pixel reduction");

API Reference

FunctionDescription
getInfoRead width, height, format, and file size
cropRemove pixels from each edge in-place
cropDirectoryBatch-crop all PNGs in a directory
resizeResize to exact pixel dimensions in-place
resizeToFitResize to fit a bounding box (preserves aspect ratio)
rotateRotate 90 / 180 / 270° clockwise in-place
flipHorizontalMirror left↔right in-place
flipVerticalMirror top↔bottom in-place
toGrayscaleConvert to grayscale in-place
convertConvert to a different format, writing a new file
thumbnailCreate a scaled copy fitting a bounding box

Comparison with Pillow

Pillowimagekit
Image.open(f).sizegetInfo(f)ImageInfo
img.crop((l, t, r, b))crop(f, top, bottom, left, right)
img.resize((w, h))resize(f, w, h)
img.thumbnail((w, h))resizeToFit(f, w, h)
img.rotate(deg)rotate(f, deg)
ImageOps.mirror(img)flipHorizontal(f)
ImageOps.flip(img)flipVertical(f)
img.convert("L")toGrayscale(f)
img.save("out.jpg")convert(src, dest, "JPEG")
img.thumbnail((w,h)); img.savethumbnail(src, dest, w, h)

Import

import tensors/imagekit;Copy

Other versions

Metadata

Released date: 7 days ago

Version: 0.1.1


Compatibility

Platform: java21

Ballerina version: 2201.13.1

GraalVM compatible: Yes


Pull count

Total: 1

Current verison: 1


Weekly downloads



Keywords

image

imaging

crop

resize

rotate

flip

grayscale

thumbnail

convert

png

jpeg


Contributors