如何使用python在白色背景上裁剪图像? [英] How do I crop an image on a white background with python?

查看:64
本文介绍了如何使用python在白色背景上裁剪图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在扫描旧照片,所以我有扫描仪中的图像和白色背景.我的目的是拍摄照片,去除白色背景.我该怎么办?

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 ?

以下是示例图片:

我的简单方法:

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))

推荐答案

在下面的示例中,我通过选择所有接近白色的像素来创建蒙版(接近,因为关注区域外的值不完全相同)白色的).然后,我反转遮罩以查找可能属于该图像的像素.然后,我计算这些像素的边界框,并用它来提取感兴趣的区域.

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()

这篇关于如何使用python在白色背景上裁剪图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆