How do I do that in Mathemtaica 8? I have found on this site many options like RemoveBackground[], but they don't work in Math.8.

Are there any options?

Here is the image; the new background should be white

enter image description here

  • 2
    $\begingroup$ Please give an example of an image. What do you want to replace the black background with? $\endgroup$
    – C. E.
    Oct 20, 2014 at 20:54

1 Answer 1


Here is a quick-and dirty solution that takes the image as the first argument and an optional tolerance within which colors are assumed to be equal to the background:

removeBackground[im_, tol_: .3] := 
   r = If[ImageColorSpace[im] != "RGB", ColorConvert[im, "RGB"], im, 
  color = ImageData[r][[2, 2]]; 
  SetAlphaChannel[r, Binarize[r, (Norm[# - color] > tol) &]]]

im = Import["https://i.stack.imgur.com/mgYjP.jpg"];



The function works by adding an alpha channel to the image in which all colors that have been deemed background (via Binarize) are transparent. For simplicity, I consider the pixel at position 2,2 to be representative of the background. One could add an option to let the user specify the background color manually, too. But the above approach is probably going to work most of the time if the background is connected to the corners.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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