6
$\begingroup$

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?

$\endgroup$
8
$\begingroup$

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}]

enter image description here

enter image description here

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

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

enter image description here

Or specify fractions explicitly:

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

enter image description here

$\endgroup$
5
$\begingroup$

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.

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

cropped

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Mr.Wizard May 21 '17 at 4:44
  • $\begingroup$ @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. $\endgroup$ – m_goldberg May 21 '17 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.