Skip to content

Crop

Cut a rectangle out of an image — by exact coordinates, or by an anchor preset.

crop_image(input, &CropOptions) -> Vec<u8>
Language Name
Rust crop_image
JavaScript cropImage
Python crop_image
CLI dusrelimage crop <input> --x 100 --y 50 --width 400 --height 300 -o out.jpg
Field Type Default Meaning
x, y u32 0 Top-left corner of the crop rectangle, in pixels.
width, height u32 required Size of the crop rectangle.
anchor Anchor or null null Preset instead of x/y: Center, TopLeft, TopRight, BottomLeft, BottomRight.

With an anchor preset, give only width and height — the position is computed from the image size. Center crops the middle; the corner presets crop from that corner.

import { cropImage } from 'dusrelimage';
// Exact rectangle
const out = cropImage(input, { x: 100, y: 50, width: 400, height: 300 });
// Centre square, whatever the input size
const square = cropImage(input, { width: 500, height: 500, anchor: 'Center' });
Terminal window
dusrelimage crop photo.jpg --x 100 --y 50 --width 400 --height 300 -o cropped.jpg
dusrelimage crop photo.jpg --width 500 --height 500 --anchor center -o square.jpg
Code When
INVALID_IMAGE Input is not a decodable image.
CROP_OUT_OF_BOUNDS The rectangle is outside the image.
INVALID_DIMENSIONS Width or height is zero.
IMAGE_TOO_LARGE Over max_pixels (default 120,000,000).
EMPTY_INPUT Empty input bytes.