|
""" |
|
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}' |
|
|
|
self._validate_input_file() |
|
|
|
|
|
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() |
|
|
|
|
|
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 |
|
|