Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The built-in ImageCrop function "effectively removes borders from image whose pixel value distribution is almost uniform" (emphasis mine). I need an efficient cropping function which removes borders whose pixel values are identical. What is the best way to do this in Mathematica? I don't need support for 3D images, but images with an alpha channel should be supported.

Here is an example of inconsistent behavior of ImageCrop (v. 9.0.1):

img = Rasterize[
   Graphics[{Circle[{0, 0}, 1], 
     Text[Style[l, FontFamily -> "Times"], {1.2, 0}]}, Frame -> False,
     PlotRange -> {{.9, 1.3}, All}], "Image", Background -> None];
Show[#, Frame -> True, FrameTicks -> False, 
   PlotRangePadding -> None] & /@ {img, ImageCrop[img], 
  ImagePad[img, -BorderDimensions[img, 0]]}


share|improve this question
This is apparently the default in Version 7: "ImageCrop[image] effectively starts at the borders of image, then removes as many layers of pixels as possible where all their color values exactly match the colors of the borders." (emphasis mine) – Mr.Wizard Mar 2 '13 at 9:11
@Mr.Wizard Such hidden changes is a source of troubles for the users. On the Documentation page of v.8 we read the same as in v.9. But on the bottom of the page in v.8 nothing is said about changes since v.7 (one can see only "New in 7"). I think that the difference in the behavior as compared to v.7 is significant. I wasted half an hour before realised that ImageCrop is "smarter" than I need! – Alexey Popkov Mar 2 '13 at 11:55
@Mr.Wizard Usually when working with new version of Mathematica I check only the bottom of the page for the function I use and if nothing is said about changes I think that there are no significant changes. But this case is critical for me! I am forced to revrite some functions I thought reliable from version to version... – Alexey Popkov Mar 2 '13 at 12:05
I agree. That's a nasty change to not announce. It would have been better to introduce a new Option for tolerance; this way old code would not break but users who wanted to could e.g. SetOptions[ImageCrop, Tolerance -> 0.03] and get the "new" behavior. – Mr.Wizard Mar 2 '13 at 12:26
@AlexeyPopkov +1 I think three small images (original, fuzzy and strict cropping) may improve the question comprehension time – Dr. belisarius Mar 2 '13 at 17:17
up vote 13 down vote accepted

I just realized that BorderDimensions accepts second argument as tolerance. So, the answer to my question is

ImagePad[img, -BorderDimensions[img, 0]]
share|improve this answer
also available here: – Yves Klett Mar 2 '13 at 21:58

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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