File size: 2,033 Bytes
424f388
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
from rembg import remove
from PIL import Image, ImageFilter
from pathlib import Path
import io
from glob import glob
from loguru import logger as log

global files_processed
files_processed = None

def get_files_processed():
    return files_processed


def remove_background_and_feather(input_path, output_path, feather_radius=4):
    # Read the image
    with open(input_path, "rb") as file:
        input_image = file.read()

    # Remove the background
    output_image = remove(input_image)

    # Convert the result to a PIL image
    img_with_transparency = Image.open(io.BytesIO(output_image))

    # Apply feather (Gaussian Blur) to the alpha channel
    alpha = img_with_transparency.getchannel("A")
    alpha_feathered = alpha.filter(ImageFilter.GaussianBlur(feather_radius))
    img_with_transparency.putalpha(alpha_feathered)

    # Save the final image
    img_with_transparency.save(output_path, "PNG")


def batch_process_images(input_dir, output_dir):
    input_dir_path = Path(input_dir)
    output_dir_path = Path(output_dir)
    output_dir_path.mkdir(exist_ok=True)
    global files_processed
    # Process each image in the input directory
    images = list(input_dir_path.glob("*.png")) + list(input_dir_path.glob("*.jpg"))
    if images:
        log.info("Found the following images: ", [img.stem for img in images])
        files_processed = len(images)
        for image_path in images:
            output_image_path = output_dir_path / image_path.name
            log.info(f"Processing {image_path}...")
            remove_background_and_feather(image_path, output_image_path)
            log.info(f"Saved to {output_image_path}")
        return files_processed
    else:
        log.info("No Images in this Directory")
        return None


def main():
    input_path = input("Enter the directory your images are in: ")
    output_path = input("Output Directory: ")

    batch_process_images(input_path, output_path)

# Usage: Specify your input and output directories
if __name__ == "__main__":
    main()