The Remover class is the primary entry point for programmatic access. You can initialize it with various settings and process images or video frames.
Initialization:
remover = Remover(mode='fast', jit=True, device='cuda:0', ckpt='~/latest.pth')
Processing Images:
out = remover.process(img, type='rgba', threshold=0.5)
Processing Video:
When processing video frames, avoid using type='rgba'. Use types like map, green, blur, etc.
Supported type values in API:
'rgba''map''green''white'[R, G, B] (e.g., [255, 0, 0])'blur''overlay''path/to/background.jpg'
import cv2
import numpy as np
from PIL import Image
from transparent_background import Remover
# 1. Initialize the remover
remover = Remover(mode='fast', jit=True, device='cuda:0')
# 2. Process an image
img = Image.open('samples/aeroplane.jpg').convert('RGB')
out = remover.process(img, type='rgba')
out.save('output.png')
# 3. Process a video
cap = cv2.VideoCapture('samples/b5.mp4')
fps = cap.get(cv2.CAP_PROP_FPS)
writer = None
while cap.isOpened():
ret, frame = cap.read()
if not ret: break
# Convert BGR to RGB for PIL
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = Image.fromarray(frame_rgb).convert('RGB')
if writer is None:
writer = cv2.VideoWriter('output.mp4', cv2.VideoWriter_fourcc(*'mp4v'), fps, img.size)
# Process frame (use 'map' for video instead of 'rgba')
out = remover.process(img, type='map')
# Convert back to BGR for OpenCV writer
writer.write(cv2.cvtColor(np.array(out), cv2.COLOR_BGR2RGB))
cap.release()
if writer:
writer.release()