Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何删除图像';背景,并使其透明?_Python_Image_Python Imaging Library - Fatal编程技术网

Python 如何删除图像';背景,并使其透明?

Python 如何删除图像';背景,并使其透明?,python,image,python-imaging-library,Python,Image,Python Imaging Library,我使用的代码如下: from PIL import Image import os path = 'C:/Users/User/Desktop/GF_BSIF/temp' newData = [] for image in os.listdir(path): img = Image.open(path+'/'+image) img = img.convert("RGBA") datas = img.getdata() for item i

我使用的代码如下:

from PIL import Image
import os

path = 'C:/Users/User/Desktop/GF_BSIF/temp'
newData = []

for image in os.listdir(path):
    img = Image.open(path+'/'+image)
    img = img.convert("RGBA")
    datas = img.getdata()

    for item in datas:
        if item[0] == 255 and item[1] == 255 and item[2] == 255:
            newData.append((255, 255, 255, 0))
        else:
            if item[0] > 150:
                newData.append((0, 0, 0, 255))
            else:
                newData.append(item)
                print(item)

img.putdata(newData)
img.save('C:/Users/User/Desktop/GF_BSIF/temp'+'/'+"open_science_logo_transparent.png", "PNG")
原件:

结果:

结果并不能使背景完全透明

如何改进代码并使黑色背景透明

编辑:

@不完全是耶里克

它显示了一个错误


TypeError回溯(最近一次调用上次)

在14 15中,对于数据中的项:-->16如果全部(对于数据[0:3]中的i,i==0):17 newData.append((0,0,0,0))18其他:

TypeError:序列索引必须是整数,而不是“切片”


如何使其正确?

您的代码告诉PIL删除所有白色像素并用黑色像素替换:
[255255255,0]
->
[0,0,0255]
,这就是为什么您的黑色背景仍然保留,而只有完整的白色斑点被删除。您需要执行相反的操作来删除背景,即
[0,0,0255]
->
[0,0,0]

这可能有用

for item in datas:
    if all(i == 0 for i in datas[0:3]):
        newData.append((0, 0, 0, 0))
    else:
        etcetcetc

您的代码告诉PIL删除并用黑色像素替换每个白色像素:
[255255255,0]
->
[0,0,0255]
,这就是为什么您的黑色背景保留下来,而只有完整的白色斑点被删除。您需要执行相反的操作来删除背景,即
[0,0,0255]
->
[0,0,0]

这可能有用

for item in datas:
    if all(i == 0 for i in datas[0:3]):
        newData.append((0, 0, 0, 0))
    else:
        etcetcetc

我会使用module
cv2
只处理
numpy.array
而不从/to
PIL
转换。这是一种交易:运行
for
-loop从文件夹中读取多个图像,但最后只保存一个文件。原始图像中的背景是什么颜色?如果不是单一颜色,这是什么?原始图像有黑色背景。我会使用module
cv2
只处理
numpy.array
,而不从/to
PIL
转换。这是一种交易:运行
for
-循环从文件夹中读取多个图像,但最后只保存一个文件。原始图像中的背景是什么颜色?如果不是单色,那是什么?原始图像有黑色背景。您的意思是
[0,0,0255]->[0,0,0,0]
而不是
[255255]->[0,0,0,0]
是的,对不起!您甚至可以尝试:if all(项目[i]<50,表示范围内的i(len(项目[0:3])):datas.append((0,0,0,0))@NotActuallyErik它显示一个错误---------------------------------------------------------------------TypeError回溯(最近一次调用)在14 15中,对于数据中的项:-->16如果全部(对于数据中的i[0:3]):17 newData.append((0,0,0,0))18其他:类型错误:序列索引必须是整数,而不是“slice”。怎么做才对?@GuanlinChen你想检查四个元素的列表中的前三个元素,显然你不能切分列表。也许索引像素列表会有帮助?也许range()和len()可以帮你?你的意思是
[0,0,0255]->[0,0,0,0]
而不是
[255255]->[0,0,0,0]
是的,对不起!您甚至可以尝试:if all(项目[i]<50,表示范围内的i(len(项目[0:3])):datas.append((0,0,0,0))@NotActuallyErik它显示一个错误---------------------------------------------------------------------TypeError回溯(最近一次调用)在14 15中,对于数据中的项:-->16如果全部(对于数据中的i[0:3]):17 newData.append((0,0,0,0))18其他:类型错误:序列索引必须是整数,而不是“slice”。怎么做才对?@GuanlinChen你想检查四个元素的列表中的前三个元素,显然你不能切分列表。也许索引像素列表会有帮助?也许range()和len()可以帮你?