使用python将图像转换为具有透明背景图像的tiff格式

问题描述

我想将jpg格式的图像转换为tiff格式。但是,我希望该图具有透明背景。我有这样的照片

enter image description here

我发现此脚本可以帮助我,但是...

from PIL import Image
import numpy as np

threshold=100
dist=5
img=Image.open("picture.jpg").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=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('out.png')
img.save('out.tiff')

但是结果是:

enter image description here

期望的图像只是背景透明的灰色和红色区域(球和棍棒)。像示例一样,如果将图像放置在任何地方,都看不到白色区域。

enter image description here

enter image description here

感谢您的帮助和支持

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)