Guide

How to Crop an Image to a Rectangular Region

Provide non-empty image data and four numeric bounds for the rectangular area to keep. A successful crop is returned as base64-encoded image data.

Tool Image Cropper

What this image cropper does

An image cropper helps you keep one rectangular part of an image while removing the surrounding area. It works with supplied image data and four numeric bounds: left, top, right, and bottom. This makes the tool useful when the crop needs to be described by repeatable coordinates instead of selected visually.

The input is image data rather than a file description or an editing instruction. The supplied value must be non-empty because the tool decodes it and attempts to open the resulting bytes as an image. If the value cannot be opened as an image, processing cannot continue successfully.

The coordinate values describe bounds against the dimensions of the decoded image. They are not percentages and are not normalized values. A coordinate such as 20 refers to a numeric position in the decoded image dimensions, according to the bound being set. The declared starting values are left 0, top 0, right 100, and bottom 100, so review them before submitting if the intended rectangle differs from those values.

How to crop an image with coordinates

  1. Prepare a non-empty value containing image data. The tool base64-decodes the supplied string and attempts to open the resulting bytes as an image. Use data that represents an image the processing step can open.

  2. Enter numeric values for left, top, right, and bottom. Treat these values as bounds applied to the decoded image dimensions. Do not convert them to percentages or normalized fractions. The initial values are left 0, top 0, right 100, and bottom 100; change them when the desired rectangle needs different bounds.

  3. Check the relationship between the bounds and the source image. The left and top values are constrained to the image bounds. The right value is constrained to the image width and then adjusted to be at least one unit greater than left. The bottom value is constrained to the image height and then adjusted to be at least one unit greater than top. These adjustments help the crop describe a non-empty rectangular area, but they also mean that a value at an image edge can affect the opposite bound.

  4. Submit the image data and coordinate values for processing. The tool decodes and opens the image, applies the rectangular crop, and saves the resulting image. Processing can fail if the image-processing dependency is unavailable or if an exception occurs while decoding, opening, cropping, or saving.

  5. Inspect the returned value after successful processing. The cropped image is returned as base64-encoded image data. When the source image format is available, the saved crop uses that format; otherwise, the saved result is PNG. Pass the returned data to the next part of your workflow in the form provided rather than treating it as a set of crop instructions.

How to interpret the result

A successful result is the selected rectangular portion of the supplied image, represented as base64 image data. It is not a list of coordinates or a description of what to edit. The image dimensions and the four bounds determine which region is retained, while the output encoding follows the source format when that format is available and falls back to PNG otherwise.

Pay attention to the boundary adjustment when reviewing a crop. Left and top remain within the decoded image bounds. Right and bottom are limited by the corresponding image width and height, then raised when necessary so each is at least one unit beyond left or top. Consequently, placing left or top at the corresponding image edge does not produce an empty extent in that direction; the opposite bound can be adjusted to create the minimum usable extent.

A failed processing attempt does not establish that the coordinates alone were wrong. The reviewed failure conditions also include an unavailable image-processing dependency, empty image data, an inability to decode or open the supplied bytes, and exceptions during cropping or saving. Check the input first, then review the numeric bounds and try again. The reviewed behavior covers rectangular cropping and output encoding; it does not establish resizing, rotation, enhancement, or user-controlled output-format selection.

Worked example

You want to keep a rectangular area from an image and describe that area with four numeric bounds.

Provide the non-empty image data, set left to 20, top to 10, right to 420, and bottom to 300, then submit the crop request.

A successful response contains base64-encoded data for the cropped image. It uses the source image format when available; otherwise, the saved result is PNG.

Limitations

  • Processing can fail during decoding, opening, cropping, or saving, and the crop bounds are constrained and adjusted against the decoded image dimensions.

Common errors

  • Cause: the image-data value is empty or its decoded bytes cannot be opened as an image. Fix: provide a non-empty value containing decodable image data, then submit it again.

FAQ

Are the crop coordinates percentages?

No. The coordinates are numeric bounds applied against the decoded image dimensions, rather than percentages or normalized values.

What format does the cropped image use?

If the source image format is available, the cropped image uses that format; otherwise, the saved result is PNG.

Why might image cropping fail?

Processing can fail when the image data is empty, when the decoded value cannot be opened as an image, when the image-processing dependency is unavailable, or when an exception occurs during decoding, opening, cropping, or saving.

Tool

Image Cropper