Image change to transparent using numpy and Pillow python

 from PIL import Image

import numpy as np

f = r"files/home.png"

image = Image.open(f)

# image.show()

# print(image.size)


arr = np.array(image)


rows = arr.shape[0]

columns = arr.shape[1]


white_ = np.array([255,255,255,255])

for row in range(rows):

    for column in range(columns):

        check_ = np.array_equal(arr[row,column],white_)

        if check_:

            arr[row,column] = 0,0,0,0



img = Image.fromarray(arr)

img.show()


img.save('files/new.png')

Comments