How do I crop an image on a white background with python?

微笑、不失礼 提交于 2021-02-07 19:57:08

问题


I am scanning old photos, so I have the image and a white background from the scanner. My aim is to take the picture, removing the white background. How can I do that ?

An example picture is the following: 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 = Image.open(image)
    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))

回答1:


In the example below, I create a mask by selecting all pixels that are close to white (close, because the values right outside the area of interest are not exactly white). I then invert the mask to find pixels potentially belonging to the image. I then calculate the bounding box 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],
            top_left[1]:bottom_right[1]]

plt.imshow(out)
plt.show()


来源:https://stackoverflow.com/questions/26310873/how-do-i-crop-an-image-on-a-white-background-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!