I want to remove this image's black background.How can I do that in python? I have already tried doing it through opencv but it doesn't work
import numpy as np
import matplotlib.pyplot as plt
import cv2
from PIL import Image
# CODE TO ELIMINATE BLACK BACKGROUND
def remove_background(image):
image = image.convert("RGBA")
datas = image.getdata()
newData = []
for item in datas:
if item[0] == 255 and item[1] == 255 and item[2] == 255:
newData.append((255,0,0,0))
else:
newData.append((item))
image.putdata(newData)
transparent_image = np.asarray(image)
return transparent_image
# remove background
bozu = Image.open("QWERTY.png")
transparent_bozu = remove_background(bozu)
from google.colab.patches import cv2_imshow
cv2_imshow(transparent_bozu)
plt.imshow(transparent_bozu)
plt.show()