Skip to content

Image

src.utils.image.downscale_image(path: Path, **kwargs) -> bool

Downscale an image using provided keyword arguments.

Parameters:

Name Type Description Default
path Path

Path to the image.

required

Returns:

Type Description
bool

True if successful, otherwise False.

Source code in src\utils\image.py
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
65
def downscale_image(path: Path, **kwargs) -> bool:
    """Downscale an image using provided keyword arguments.

    Args:
        path: Path to the image.

    Keyword Args:
        width (int): Maximum width, default: 3264
        optimize (bool): Whether to use Pillow optimize, default: True
        quality (int): JPEG quality, 1-100, default: 3264
        resample (Resampling): Resampling algorithm, default: LANCZOS

    Returns:
        True if successful, otherwise False.
    """
    # Establish our source and destination directories
    path_out = Path(path.parent, 'compressed')
    path_out.mkdir(mode=777, parents=True, exist_ok=True)
    save_path = Path(
        path_out, kwargs.get('name', path.name)
    ).with_suffix('.jpg')

    # Establish our optional parameters
    max_width = kwargs.get('max_width', 3264)
    optimize = kwargs.get('optimize', True)
    quality = kwargs.get('quality', 95)

    # Open the image, get dimensions
    with Image.open(path) as image:

        # Convert to RGB
        image.convert('RGB')

        # Calculate dimensions
        width, height = image.size
        if width > max_width:
            image.thumbnail(
                size=(max_width, round((height * max_width) / width)),
                resample=kwargs.get('resample', Resampling.LANCZOS))

        # Save the new image
        try:
            image.save(
                fp=save_path,
                format='JPEG',
                quality=quality,
                optimize=optimize)
        except OSError:
            return False
        return True