I'm looking for a script (with Imagemagick or python?) that can crop an image to fit the foreground image (non white pixels that are connected).
Basically, selecting white background, inverting the selection and cropping to the nearest rectangular image.
Example: this image with white background:
should become this image: