# image/crop
Crop to the rectangle `($x, $y, $w, $h)` in pixels, returning a new value of
the same type (`Image` or `PixelBuffer`). `$x`/`$y` are the top-left corner
(0-based); `$w`/`$h` are the size. The size is clamped so the rectangle never
runs past the image bounds, so `[image/crop $img 6 4 100 100]` on an 8×6
image yields a 2×2 crop.
## Syntax
[image/crop $v $x $y $w $h]
## Parameters
- `$v` — an `Image` or `PixelBuffer` value
- `$x`, `$y` — top-left corner in pixels (integers, must be inside the image)
- `$w`, `$h` — crop size in pixels (integers, at least 1; clamped to the
remaining width/height)
## Examples
set img [image/open "test/fixtures/images/test.png"] [image/dims [image/crop $img 1 1 4 3]] # → %{ :w 4 :h 3 } [image/dims [image/crop $img 6 4 100 100]] # → %{ :w 2 :h 2 } (clamped) [image/crop $img 8 0 1 1] # → %Err{:message "image/crop: origin outside image"} [image/crop $img 0 0 0 3] # → %Err{:message "image/crop: width/height must be positive"} set buf [image/load "test/fixtures/images/test.png"] [image/dims [image/crop $buf 1 1 4 3]] # → %{ :w 4 :h 3 }
## Returns
A new `Image`/`PixelBuffer`; the source is unchanged. `%Err{:message}` when
`$x`/`$y` are outside the image or `$w`/`$h` are below 1. `nil` when `$v` is
neither an `Image` nor a `PixelBuffer`.
## See Also
`image/resize`, `image/rotate`, `image/encode`, `image/dims`