python怎么弄成白色背景,使用PIL python将白色背景转换为透明背景

阅读: 评论:0

python怎么弄成白色背景,使用PIL python将白色背景转换为透明背景

python怎么弄成白色背景,使用PIL python将白色背景转换为透明背景

How can i transform all white background and white elements of a png or jpg image in a transparent backgroun using PIL?

解决方案

Using numpy, the following makes white-ish areas transparent. You can change threshold and dist to control the definition of "white-ish".

import Image

import numpy as np

threshold=100

dist=5

img=Image.open(FNAME).convert('RGBA')

# np.asarray(img) is read only. Wrap it in np.array to make it modifiable.

arr=np.array(np.asarray(img))

r,g,b,a&#llaxis(arr,axis=-1)

mask=((r>threshold)

& (g>threshold)

& (b>threshold)

& (np.abs(r-g)

& (np.abs(r-b)

& (np.abs(g-b)

)

arr[mask,3]=0

img=Image.fromarray(arr,mode='RGBA')

img.save('/tmp/out.png')

The code is easy to modify so that only RGB value (255,255,255) is turned transparent -- if that is what you truly want. Simply change the mask to:

mask=((r==255)&(g==255)&(b==255)).T

本文发布于:2024-02-04 23:16:29,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170718638560667.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:背景   白色   转换为   弄成   透明
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23