# Cropping from the top and bottom of an image using ImageCrop

Under details and options the documentation article ref/ImageCrop says:

The cropping specification spec in ImageCrop[image, size, spec] can have the following forms:

{side1, side2} ... crops on the specified sides

and

Sides can be specified as Left, Right, Bottom, or Top. Center indicates symmetrical cropping.

and yet

ImageCrop[image, 2, {Top, Bottom}]


produces the message

{Top, Bottom} is not a list of two numbers or two valid symbolic cropping values.

What am I missing?

The documentation could be more clear I think, and the syntax message isn't very helpful. I believe the specifications must be Left or Right, Bottom or Top. So for example:

img = Image[DiamondMatrix[50]]

ImageCrop[img, {70, 70}, {Left, Top}]


If you want symmetrical top-and-bottom cropping use Center:

ImageCrop[img, {70, 70}, {Right, Center}]


Or specify fractions explicitly:

ImageCrop[img, {70, 70}, {-2/3, 0}]


I agree that the documentation of ImageCrop is not nearly as clear as it could be.

What you are missing is that the 3rd argument, the side specification, must relate to the 2nd argument, the size specification. The size spec is always given in the form

{width_crop, height_crop}


so giving

{Top, Bottom}


is rejected because Top can not be related to the width of the image.

To crop a certain number of pixels off from both the top and the bottom of an while leaving the width alone, we must coordinate the size spec with the side spec. Consider removing 25 pixels both the top and the bottom of the following image.

One way is to write

With[{pxls = 25, img = < image >},
ImageCrop[img, {Full, ImageDimensions[img][[2]] - 2 pxls}, Center]]


where < image > is the image shown above. This produces

• The last operation could be done more cleanly with ImageTake[img, {26, -26}]. That doesn't teach one about ImageCrop of course, other than it's best to start with the right tool. +1 for making the edge specification explicit; I meant to imply that but reading again I see there is still room for confusion in what I wrote. – Mr.Wizard May 21 '17 at 4:44
• @Mr.Wizard.1) I agree that it might be better to use ImageTake, but I think it is quite natural for users to turn to ImageCrop when they want to crop an image. 2) I would not of posted this answer if I hadn't thought your answer needed some clarification. – m_goldberg May 21 '17 at 15:38