File size: 1,331 Bytes
609d54c
 
 
 
 
 
 
05ba985
609d54c
 
 
05ba985
c8ef5ef
 
 
 
 
 
 
 
 
 
05ba985
c8ef5ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""
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