ImageCrop

ImageCrop[image]

crops image by removing borders of uniform color.

ImageCrop[image,size]

crops image based on the size specification size.

ImageCrop[image,size,spec]

crops image by removing pixels from sides specified by spec.

Details and Options

  • Image cropping, also known as image trimming, is the process of removing border pixels from an image and can be used for removing uninteresting regions or extracting regions of interest.
  • ImageCrop[image] effectively removes borders from image whose pixel value distribution is almost uniform.
  • ImageCrop[image,{width,height}] by default removes pixels symmetrically from image, or if width or height is larger than for image, it adds pixels symmetrically.
  • The image size specification size in ImageCrop[image,size] can have the following forms:
  • widthparticular width; height based on aspect ratio
    {width,height}particular width and height
    {width,depth,height}particular width, depth and height in 3D
  • Specifications for either of the dimensions can be any of the following:
  • dd pixels
    UpTo[d]maximum d pixels
    {dmin,dmax}pixel ranges
    Automaticdetermine value from aspect ratio
    Fulldo no cropping in this dimension
  • The cropping specification spec in ImageCrop[image,size,spec] can have the following forms:
  • sidecrop on the specified side; center otherwise
    {side1,side2}crop on the specified sides
    {ax,ay}assign a fraction of cropping to each side
  • Sides can be specified as Left, Right, Bottom or Top. Center indicates symmetrical cropping.
  • Cropping fractions are between and . In the horizontal case, is equivalent to Left, to Right and to Center. In the vertical case, is equivalent to Bottom, and to Top.
  • ImageCrop[image,size] is equivalent to ImageCrop[image,size,Center].
  • To obtain an image with an aspect ratio r, use ImageCrop[image,width{1,r}].
  • If the final image specified by ImageCrop[image,] extends beyond image, then pixels are added according to the setting for Padding.

Examples

open allclose all

Basic Examples  (2)

Automatically crop uniform borders:

Crop an image to a given size:

Scope  (6)

Data  (2)

Crop a 2D image:

Crop constant areas of a 3D image:

Size Specification  (2)

Crop an image to a width of 100 pixels, preserving the aspect ratio:

Crop to a 100×100 pixel square:

Do not crop the height of the image:

Crop to a height of 100 pixels, preserving the aspect ratio:

Make the largest dimension not greater than 100 pixels:

Pad the image by using values greater than the image dimensions:

Crop a 3D image:

Crop a 3D image in the vertical dimension only:

Side Specification  (2)

Crop on the left side, preserving the aspect ratio:

Crop at the left side, using numerical values:

Crop evenly from both sides using numerical values:

Crop from the left and bottom sides:

Crop a 3D image:

Crop the top 23 slices off a 3D image:

Extract a selected horizontal slice of a 3D image:

Take all slices in a selected region of the 3D image:

Options  (2)

Padding  (2)

By default, black padding is used:

Pad the image with a specified color:

Specify a different padding scheme:

Use Padding->None to avoid padding:

The result may be smaller than the given size:

With Padding->Automatic, a color based on the boundary pixel values is chosen:

If the boundary pixels do not have consistent colors, black is used:

Applications  (1)

Letterbox a wide image into a 320×240 format:

Properties & Relations  (3)

ImageCrop preserves the aspect ratio of the image:

Use ImageTake to crop an image:

ImageCrop adds or removes pixels on the sides to achieve specified output dimensions:

Use ImagePad to add or remove specified numbers of pixels on the sides:

Possible Issues  (2)

Cropping happens if a similar border is present on at least two sides:

If a border is present only on one side, it is not cropped:

Detect the border using BorderDimensions and crop it using ImagePad:

Introduced in 2008
 (7.0)
 |
Updated in 2014
 (10.0)
2015
 (10.3)