Automatically cropping an image with python/PIL

Install Pillow

pip install Pillow

and use as

from PIL import Image
    
image=Image.open('L_2d.png')

imageBox = image.getbbox()
cropped = image.crop(imageBox)
cropped.save('L_2d_cropped.png')

When you search for boundaries by mask=imageComponents[3], you search only by blue channel.

Leave a Comment