-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_bg.py
More file actions
25 lines (21 loc) · 840 Bytes
/
remove_bg.py
File metadata and controls
25 lines (21 loc) · 840 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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] > 200 and item[1] > 200 and item[2] > 200:
newData.append((255, 255, 255, 0))
else:
newData.append(item)
img.putdata(newData)
img.save(output_path, "PNG")
print(f"Successfully saved transparent image to {output_path}")
except Exception as e:
print(f"Error processing image: {e}")
if __name__ == "__main__":
remove_background("frontend/public/playwright_masks.png", "frontend/public/playwright_masks_transparent.png")