python去除图片背景,使用Python / PIL从图像中删除背景色

I've been trying to get this to work and am really having trouble, so would be very grateful for some help.

Using the code below, I want to change the features with the specified RGB values to white, and all the other features in the image black (i.e. basically extracting the features from the image. Unfortunately, although I can make the features I want to 'extract' fine, when I try to remove the background colours (I'd been trying to use

mask2 = ((red != r1) & (green != g1) & (blue != b1))

data[:,:,:4][mask2] = [rb, gb, bb, ab]

but that seems to select any pixels except those with red == r1 OR green == g1 etc, leaving me with a background image that is quite 'noisy'.) Does anyone know a way to literally extract those pixels with the specified RGB values, or a better way to recolour the background pixel

你可能感兴趣的:(python去除图片背景)