Files
SaaS-PDF/backend/app/services/removebg_service.py
Your Name 6bb76e3f1b Add OCR, Background Removal, and PDF Editor features with tests
- Implemented OCR functionality using pytesseract for image and PDF text extraction.
- Added Background Removal service using rembg for image processing.
- Developed PDF Editor service for applying text annotations to PDF files.
- Created corresponding API routes for OCR, Background Removal, and PDF Editor.
- Added frontend components for OCR and Background Removal tools.
- Integrated feature flagging for new tools, ensuring they are disabled by default.
- Implemented comprehensive unit tests for OCR service, PDF editor, and background removal.
- Updated documentation to reflect new features and usage instructions.
- Added translations for new features in English, Arabic, and French.
2026-03-07 21:29:08 +02:00

61 lines
1.7 KiB
Python

"""Background removal service using rembg."""
import logging
import os
from PIL import Image
logger = logging.getLogger(__name__)
class RemoveBGError(Exception):
"""Custom exception for background removal failures."""
pass
def remove_background(input_path: str, output_path: str) -> dict:
"""Remove the background from an image.
Args:
input_path: Path to the input image.
output_path: Path for the output PNG (always PNG — transparency).
Returns:
dict with ``original_size``, ``output_size``, ``width``, ``height``.
Raises:
RemoveBGError: If the operation fails.
"""
os.makedirs(os.path.dirname(output_path), exist_ok=True)
try:
from rembg import remove as rembg_remove
with Image.open(input_path) as img:
if img.mode != "RGBA":
img = img.convert("RGBA")
width, height = img.size
original_size = os.path.getsize(input_path)
result = rembg_remove(img)
result.save(output_path, format="PNG", optimize=True)
output_size = os.path.getsize(output_path)
logger.info(
"Background removed: %s%s (%d%d bytes)",
input_path, output_path, original_size, output_size,
)
return {
"original_size": original_size,
"output_size": output_size,
"width": width,
"height": height,
}
except ImportError:
raise RemoveBGError("rembg is not installed.")
except (IOError, OSError) as e:
raise RemoveBGError(f"Background removal failed: {str(e)}")
except Exception as e:
raise RemoveBGError(f"Background removal failed: {str(e)}")