1from PIL import Image
2
3img = Image.open('img.png')
4img = img.convert("RGBA")
5datas = img.getdata()
6
7newData = []
8for item in datas:
9 if item[0] == 255 and item[1] == 255 and item[2] == 255:
10 newData.append((255, 255, 255, 0)) # This is for checking white pixels, replace transparent. Do
11 # if item[3] == 0 to check for transparent pixels.
12 else:
13 newData.append(item)
14
15img.putdata(newData)
16img.save("img2.png", "PNG")