({{, (RefLink[ImageCrop, paclet:ref/ImageCrop])[image] 
crops image by removing borders of uniform color. }, {, (RefLink[ImageCrop, paclet:ref/ImageCrop])[image,size]
crops image based on the size specification size.}, {, (RefLink[ImageCrop, paclet:ref/ImageCrop])[image,size,spec]
crops image by removing pixels from sides specified by spec.}})

OpenersNotesSection(Spacer1)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.

(RefLink[ImageCrop, paclet:ref/ImageCrop])[image] effectively removes borders from image whose pixel value distribution is almost uniform.

(RefLink[ImageCrop, paclet:ref/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 (RefLink[ImageCrop, paclet:ref/ImageCrop])[image,size] can have the following forms:

Specifications for either of the dimensions can be any of the following:

The cropping specification spec in (RefLink[ImageCrop, paclet:ref/ImageCrop])[image,size,spec] can have the following forms:

Sides can be specified as (RefLink[Left, paclet:ref/Left]), (RefLink[Right, paclet:ref/Right]), (RefLink[Bottom, paclet:ref/Bottom]) or (RefLink[Top, paclet:ref/Top]). (RefLink[Center, paclet:ref/Center]) indicates symmetrical cropping.

Cropping fractions are between -1 and +1. In the horizontal case, -1 is equivalent to (RefLink[Left, paclet:ref/Left]), +1 to (RefLink[Right, paclet:ref/Right]) and 0 to (RefLink[Center, paclet:ref/Center]). In the vertical case, -1 is equivalent to (RefLink[Bottom, paclet:ref/Bottom]), and +1 to (RefLink[Top, paclet:ref/Top]).

(RefLink[ImageCrop, paclet:ref/ImageCrop])[image,size] is equivalent to (RefLink[ImageCrop, paclet:ref/ImageCrop])[image,size,(RefLink[Center, paclet:ref/Center])].

To obtain an image with an aspect ratio r, use (RefLink[ImageCrop, paclet:ref/ImageCrop])[image,width{1,r}].

If the final image specified by (RefLink[ImageCrop, paclet:ref/ImageCrop])[image,…] extends beyond image, then pixels are added according to the setting for (RefLink[Padding, paclet:ref/Padding]).

OpenersPrimaryExamplesSection(Spacer1)Examples  (16)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

Automatically crop uniform borders:

ImageCrop[]

Crop an image to a given size:

ImageCrop[,{200,70}]

OpenersExampleSection1(Spacer1)Scope  (6)

OpenersExampleSubsection0(Spacer1)Data  (2)

Crop a 2D image:

ImageCrop[,{200,80}]

Crop constant areas of a 3D image:

ImageCrop[]

OpenersExampleSubsection1(Spacer1)Size Specification  (2)

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

i=;
ImageCrop[i,100]

Crop to a 100×100 pixel square:

ImageCrop[i,{100,100}]

Do not crop the height of the image:

ImageCrop[i,{100,Full}]

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

ImageCrop[i,{Automatic,100}]

Make the largest dimension not greater than 100 pixels:

ImageCrop[ImageRotate[i],UpTo[100]]

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

ImageCrop[i,250]

Crop a 3D image:

i=;

ImageCrop[i,23]

Crop a 3D image in the vertical dimension only:

ImageCrop[i,{Full,Full,23}]

OpenersExampleSubsection2(Spacer1)Side Specification  (2)

Crop on the left side, preserving the aspect ratio:

ImageCrop[i,150,Left]

Crop at the left side, using numerical values:

ImageCrop[i,150,{-1,0}]

Crop evenly from both sides using numerical values:

ImageCrop[i,150,{-0.5,0}]

Crop from the left and bottom sides:

ImageCrop[i,150, {Left,Bottom}]

Crop a 3D image:

i=;

Crop the top 23 slices off a 3D image:

ImageCrop[i,{Full,Full,23},Top]

Extract a selected horizontal slice of a 3D image:

ImageCrop[i,{Full,Full,1},{0,0,0.5}]

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

ImageCrop[i,{23,23,Full},{-1,1,1}]

OpenersExampleSection2(Spacer1)Options  (2)

OpenersExampleSubsection3(Spacer1)Padding  (2)

By default, black padding is used:

i=;
ImageCrop[i,{200,100}]

Pad the image with a specified color:

ImageCrop[i,{200,100},Padding->Red]

Specify a different padding scheme:

ImageCrop[i,{200,100},Padding->"Fixed"]

Use (RefLink[Padding, paclet:ref/Padding])->(RefLink[None, paclet:ref/None]) to avoid padding:

ImageCrop[i,{200,100},Padding->None]

The result may be smaller than the given size:

ImageDimensions[%]

With (RefLink[Padding, paclet:ref/Padding])->(RefLink[Automatic, paclet:ref/Automatic]), a color based on the boundary pixel values is chosen:

ImageCrop[,{300,200},Padding->Automatic]

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

ImageCrop[,{300,200},Padding->Automatic]

OpenersExampleSection3(Spacer1)Applications  (1)

Letterbox a wide image into a 320×240 format:

{w,h}={320,240};
ImageCrop[ImageResize[,{{w},{h}}],{w,h}]

OpenersExampleSection4(Spacer1)Properties & Relations  (3)

(RefLink[ImageCrop, paclet:ref/ImageCrop]) preserves the aspect ratio of the image:

ImageCrop[,100]

Use (RefLink[ImageTake, paclet:ref/ImageTake]) to crop an image:

i=;
{w,h}=ImageDimensions[i];
ImageCrop[i,{100,50}]==ImageTake[i,{1,-1} ((w-100)/2+1),{1,-1} ((h-50)/2+1)]

(RefLink[ImageCrop, paclet:ref/ImageCrop]) adds or removes pixels on the sides to achieve specified output dimensions:

ImageCrop[,{190,60},Padding->Yellow]

Use (RefLink[ImagePad, paclet:ref/ImagePad]) to add or remove specified numbers of pixels on the sides:

ImagePad[,{{20,20},{-20,-20}},Padding->Yellow]

OpenersExampleSection5(Spacer1)Possible Issues  (2)

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

ImageCrop[]

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

i=;

ImageCrop[i]

Detect the border using (RefLink[BorderDimensions, paclet:ref/BorderDimensions]) and crop it using (RefLink[ImagePad, paclet:ref/ImagePad]):

BorderDimensions[i]

ImagePad[i,-%]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[ImageTrim, paclet:ref/ImageTrim])  ▪  (RefLink[ImagePad, paclet:ref/ImagePad])  ▪  (RefLink[ImageTake, paclet:ref/ImageTake])  ▪  (RefLink[BorderDimensions, paclet:ref/BorderDimensions])  ▪  (RefLink[ImagePartition, paclet:ref/ImagePartition])  ▪  (RefLink[ImageSize, paclet:ref/ImageSize])  ▪  (RefLink[ImageAugmentationLayer, paclet:ref/ImageAugmentationLayer])}})}})

({{DocumentationFooterIconsRelatedTutorial.png, ({{Tutorials}, {▪(RefLinkPlain[Image Processing, paclet:tutorial/ImageProcessing])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Basic Image Manipulation, paclet:guide/BasicImageManipulation])}, {▪(RefLinkPlain[Geometric Operations, paclet:guide/ImageGeometry])}, {▪(RefLinkPlain[Computational Photography, paclet:guide/ComputationalPhotography])}})}})