25 lines
871 B
Python
25 lines
871 B
Python
from PIL import Image
|
|
|
|
def remove_background(input_path, output_path):
|
|
try:
|
|
img = Image.open(input_path)
|
|
img = img.convert("RGBA")
|
|
datas = img.getdata()
|
|
|
|
newData = []
|
|
for item in datas:
|
|
# Change all white (also shades of whites)
|
|
# to transparent
|
|
if item[0] > 240 and item[1] > 240 and item[2] > 240:
|
|
newData.append((255, 255, 255, 0))
|
|
else:
|
|
newData.append(item)
|
|
|
|
img.putdata(newData)
|
|
img.save(output_path, "PNG")
|
|
print("Successfully removed background")
|
|
except Exception as e:
|
|
print(f"Error: {e}")
|
|
|
|
remove_background("c:\\Users\\timo\\Documents\\Websites\\website-monitor\\frontend\\public\\logo.png", "c:\\Users\\timo\\Documents\\Websites\\website-monitor\\frontend\\public\\logo.png")
|