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

本文介绍了一种使用Python和PIL库从图像中提取特定颜色特征并替换背景的方法。通过比较RGB值,将指定颜色替换为白色,其余部分替换为黑色,从而实现背景的去除。
摘要由CSDN通过智能技术生成

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值