File size: 1,279 Bytes
78a2475
24df615
961cd54
 
 
78a2475
982bffd
 
78a2475
 
 
 
961cd54
 
 
 
 
 
 
78a2475
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Convert images without silently discarding alpha in PNG or GIF output."""

from pathlib import Path
from shutil import copyfile

from PIL import Image


def convert_image_format(input_path: str, output_path: str, image_format: str = "JPEG") -> None:
    fmt = image_format.upper()
    if fmt not in ("JPEG", "PNG", "GIF"):
        raise ValueError("Unsupported output format")
    # Re-encoding an animated GIF as a single frame would destroy animation.
    if fmt == "GIF":
        with Image.open(input_path) as source:
            if getattr(source, "is_animated", False):
                if Path(input_path).resolve() != Path(output_path).resolve():
                    copyfile(input_path, output_path)
                return
    with Image.open(input_path) as original:
        image = original.copy()
    if fmt == "JPEG":
        if image.mode in ("RGBA", "LA") or "transparency" in image.info:
            rgba = image.convert("RGBA")
            background = Image.new("RGB", rgba.size, "white")
            background.paste(rgba, mask=rgba.getchannel("A"))
            image = background
        else:
            image = image.convert("RGB")
    elif fmt == "GIF":
        image = image.convert("RGBA").quantize(colors=255)
    image.save(output_path, format=fmt)