How to crop an image on a white background using python?

I am reviewing old photos, so I have an image and a white background from a scanner. My goal is to take a photo by removing the white background. How can i do this?

An example example: enter image description here

My simple approach:

import os
import time
from PIL import Image
from collections import Counter
import numpy as np

def get_cropped_image(image, crop_folder, threshold):
    image_name = image.split("\\")[-1]
    im =
    pixels = im.load()
    width, height = im.size

    rows = []
    for h_index in xrange(height):
        row = []
        for w_index in xrange(width):
            row.append(pixels[((w_index, h_index))])
        color_count = Counter(row)[(255, 255, 255)] / float(len(row))
        rows.append([h_index, color_count])

    columns = []
    for w_index in xrange(width):
        column = []
        for h_index in xrange(height):
            column.append(im.getpixel((w_index, h_index)))
        color_count = Counter(column)[(255, 255, 255)] / float(len(column))
        columns.append([w_index, color_count])

    image_data = csv.writer(open("image_data.csv", "wb")).writerows(zip(rows, columns))

    rows_indexes = [i[0] for i in rows if i[1] < threshold]
    columns_indexes = [i[0] for i in columns if i[1] < threshold]

    x1, y1, x2, y2 = columns_indexes[0], rows_indexes[0], columns_indexes[-1], rows_indexes[-1]

    im.crop((x1, y1, x2, y2)).save(os.path.join(cropped_folder, "c_" + image_name))



source to share

1 answer

In the example below, I create a mask by selecting all pixels that are close to white (close because values ​​outside the region of interest are not quite white). Then I invert the mask to find pixels that are potentially in the image. Then I calculate the bounding rectangle of those pixels and use it to extract the region of interest.

from skimage import io, img_as_float
import matplotlib.pyplot as plt
import numpy as np

image = img_as_float(io.imread('universe.jpg'))

# Select all pixels almost equal to white
# (almost, because there are some edge effects in jpegs
# so the boundaries may not be exactly white)
white = np.array([1, 1, 1])
mask = np.abs(image - white).sum(axis=2) < 0.05

# Find the bounding box of those pixels
coords = np.array(np.nonzero(~mask))
top_left = np.min(coords, axis=1)
bottom_right = np.max(coords, axis=1)

out = image[top_left[0]:bottom_right[0],





All Articles