Python 如何使用PIL在单个白色背景上检测和裁剪多个图像?

Python 如何使用PIL在单个白色背景上检测和裁剪多个图像?,python,image,python-imaging-library,whitespace,crop,Python,Image,Python Imaging Library,Whitespace,Crop,我刚刚开始使用PIL,我需要使用PIL在一个白色背景上检测和裁剪多个图像的帮助。它们可以是不同的尺寸,位于不同的位置。现在,我只能裁剪出一张图片。 请帮忙,谢谢 def trim(im): bg = Image.new(im.mode, im.size, im.getpixel((0,0))) diff = ImageChops.difference(im, bg) diff = ImageChops.add(diff, diff, 2.0, -100) bbox

我刚刚开始使用PIL,我需要使用PIL在一个白色背景上检测和裁剪多个图像的帮助。它们可以是不同的尺寸,位于不同的位置。现在,我只能裁剪出一张图片。 请帮忙,谢谢

def trim(im):
    bg = Image.new(im.mode, im.size, im.getpixel((0,0)))
    diff = ImageChops.difference(im, bg)
    diff = ImageChops.add(diff, diff, 2.0, -100)
    bbox = diff.getbbox()
    if bbox:
        return im.crop(bbox)
    else:
        print("No image detected")

image1 = Image.open('multiple.jpg')
image2 = test(image1)

这可以提高效率,但这会使答案复杂化

from PIL import Image, ImageChops

def crop(im, white):
    bg = Image.new(im.mode, im.size, white)
    diff = ImageChops.difference(im, bg)
    bbox = diff.getbbox()
    if bbox:
        return im.crop(bbox)

def split(im, white):
    # Is there a horizontal white line?
    whiteLine = Image.new(im.mode, (im.width, 1), white)
    for y in range(im.height):
        line = im.crop((0, y, im.width, y+1))
        if line.tobytes() == whiteLine.tobytes():
            # There is a white line
            # So we can split the image into two
            # and for efficiency, crop it
            ims = [
                crop(im.crop((0, 0, im.width, y)), white),
                crop(im.crop((0, y+1, im.width, im.height)), white)
            ]
            # Now, because there may be white lines within the two subimages
            # Call split again, making this recursive
            return [sub_im for im in ims for sub_im in split(im, white)]

    # Is there a vertical white line?
    whiteLine = Image.new(im.mode, (1, im.height), white)
    for x in range(im.width):
        line = im.crop((x, 0, x+1, im.height))
        if line.tobytes() == whiteLine.tobytes():
            ims = [
                crop(im.crop((0, 0, x, im.height)), white),
                crop(im.crop((x+1, 0, im.width, im.height)), white)
            ]
            return [sub_im for im in ims for sub_im in split(im, white)]

    # There are no horizontal or vertical white lines
    return [im]

def trim(im):
    # You have defined the pixel at (0, 0) as white in your code
    white = im.getpixel((0,0))

    # Initial crop
    im = crop(im, white)
    if im:
        return split(im, white)
    else:
        print("No image detected")

image1 = Image.open('multiple.jpg')
trim(image1)

请编辑您的问题,使您的代码是一个最小的完整的可验证示例,而不是一个片段。并测试它,以确保它显示您的问题。提示:没有调用trim()函数,这至少是一个问题。