File size: 1,329 Bytes
59e40e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Source url: https://github.com/OPHoperHPO/image-background-remove-tool
Author: Nikita Selin (OPHoperHPO)[https://github.com/OPHoperHPO].
License: Apache License 2.0
"""
from pathlib import Path
from PIL import Image
import warnings
from typing import Optional


def save_file(output: Optional[Path], input_path: Path, image: Image.Image):
    """
    Saves an image to the file system

    Args:
        output: Output path [dir or end file]
        input_path: Input path of the image
        image: Image to be saved.
    """
    if isinstance(output, Path) and str(output) != "none":
        if output.is_dir() and output.exists():
            image.save(output.joinpath(input_path.with_suffix(".png").name))
        elif output.suffix != "":
            if output.suffix != ".png":
                warnings.warn(
                    f"Only export with .png extension is supported! Your {output.suffix}"
                    f" extension will be ignored and replaced with .png!"
                )
            image.save(output.with_suffix(".png"))
        else:
            raise ValueError("Wrong output path!")
    elif output is None or str(output) == "none":
        image.save(
            input_path.with_name(
                input_path.stem.split(".")[0] + "_bg_removed"
            ).with_suffix(".png")
        )