Home > AI > Language > Python > PIL >

crop away transparent background

from PIL import Image
import glob


def crop(img_name):
    im = Image.open(img_name)
    im2 = im.crop(im.getbbox())
    im2.save(img_name[:-4]+'.png')

fs = glob.glob('*.jpg')
for f in fs:
    crop(f)


Save as .png rather than .jpg because jpg doesn’t support Alpha channel.

Leave a Reply