Qu

crop

Builtin index · Images

result = crop(img, x0, y0, width, height)

Input: img, x0, y0, width, height · Output: result

What it is

img is an Image; x0, y0 are integers giving the top-left pixel coordinate of the sub-rectangle; width/height are positive integers giving its size in pixels. Extracts that sub-rectangle.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = crop(img, x0, y0, width, height)
img Imageimg is an Image.
x0 —x0, y0 are integers giving the top-left pixel coordinate of the sub-rectangle.
y0 —x0, y0 are integers giving the top-left pixel coordinate of the sub-rectangle.
width —width/height are positive integers giving its size in pixels.
height —width/height are positive integers giving its size in pixels.
result Image
Returns a new Image of exactly width x height pixels.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

canvas = image_new(40, 30, r=200, g=200, b=200)

m = [10, 40, 250; 90, 140, 190; 220, 255, 30]
gray = image_from_matrix(m)
save_image("/tmp/gray.bmp", gray)

imagesc(m, colormap="viridis")          # false-color view of the same matrix


big = resize(canvas, 80, 60, method="bilinear")
piece = crop(big, 10, 10, 30, 20)
print(piece.width, piece.height)
imshow(piece)

from images

How it works

crop(img, x0, y0, width, height) — extract a sub-rectangle.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.