Tools / image /remove_background.py
jebin2's picture
remove bg added
05ba985
"""
Background Remover Module
This module provides a high-quality, class-based background remover using rembg.
"""
from rembg import remove
from .image_base import ImageBase
from custom_logger import logger_config
class RemoveBackground(ImageBase):
"""
High-quality background remover using the rembg library.
This class provides background removal functionality for supported image formats,
with proper validation, logging, and output management.
"""
def __init__(self):
super().__init__("remove_background")
def process(self, input_file_name: str) -> str:
try:
self.input_file_name = input_file_name
self.input_file_path = f'{self.input_dir}/{self.input_file_name}'
# Validate input
self._validate_input_file()
# Generate output path
output_path = self._generate_output_path()
logger_config.info(f"Removing background from: {self.input_file_path}")
with open(self.input_file_path, 'rb') as infile:
input_image = infile.read()
# Perform background removal
output_image = remove(input_image)
with open(output_path, 'wb') as outfile:
outfile.write(output_image)
logger_config.info(f"Background removed successfully: {output_path}")
return output_path
except Exception as e:
logger_config.error(f"Background removal failed: {str(e)}")
return None