app.py
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 |
import os from rembg import remove from PIL import Image import io def batch_remove_bg(input_folder, output_folder): # Create output folder if it doesn't exist os.makedirs(output_folder, exist_ok=True) # Process each image in the input folder for filename in os.listdir(input_folder): if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.webp')): input_path = os.path.join(input_folder, filename) output_path = os.path.join(output_folder, os.path.splitext(filename)[0] + ".png") with open(input_path, 'rb') as f: input_bytes = f.read() output_bytes = remove(input_bytes) output_image = Image.open(io.BytesIO(output_bytes)) output_image.save(output_path) print(f"Processed: {filename} ➜ {output_path}") # Example usage batch_remove_bg("input", "output_images") |