replace transparent pixels python 1

replace transparent pixels python

from PIL import Image

img ='img.png')
img = img.convert("RGBA")
datas = img.getdata()

newData = []
for item in datas:
    if item[0] == 255 and item[1] == 255 and item[2] == 255:
        newData.append((255, 255, 255, 0)) # This is for checking white pixels, replace transparent. Do
        # if item[3] == 0 to check for transparent pixels.

img.putdata(newData)"img2.png", "PNG")

Here is what the above code is Doing:
1. Open the image
2. Convert it to RGBA
3. Get the data from the image
4. Create a new list
5. Loop through the data
6. If the pixel is white, make it transparent
7. If the pixel is not white, add it to the new list
8. Put the new data into the image
9. Save the image

Similar Posts