Using numpy, the following makes white areas transparent. You can change thresholdand distto control the definition of "white-ish".
import Image
import numpy as np
threshold=100
dist=5
img=Image.open(FNAME).convert('RGBA')
arr=np.array(np.asarray(img))
r,g,b,a=np.rollaxis(arr,axis=-1)
mask=((r>threshold)
& (g>threshold)
& (b>threshold)
& (np.abs(r-g)<dist)
& (np.abs(r-b)<dist)
& (np.abs(g-b)<dist)
)
arr[mask,3]=0
img=Image.fromarray(arr,mode='RGBA')
img.save('/tmp/out.png')
The code is easy to modify, so only the RGB value (255,255,255) becomes transparent - if that's what you really want. Just change maskto:
mask=((r==255)&(g==255)&(b==255)).T
source
share