generated from cotes2020/chirpy-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_to_webp.py
55 lines (44 loc) · 1.57 KB
/
convert_to_webp.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
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
import os
import sys
import subprocess
from typing import NoReturn
def convert_images_to_webp(source_folder: str) -> None:
# Check if the source folder exists
if not os.path.exists(source_folder):
print(f"The folder {source_folder} does not exist.")
return
# Create a new folder inside the source folder for the WebP files
destination_folder: str = os.path.join(source_folder, "webp_images")
if not os.path.exists(destination_folder):
os.makedirs(destination_folder)
# Loop through each file in the source folder
for filename in os.listdir(source_folder):
if filename.lower().endswith(
(".jpeg", ".jpg")
): # Add more extensions if needed
source_path: str = os.path.join(source_folder, filename)
destination_path: str = os.path.join(
destination_folder, f"{os.path.splitext(filename)[0]}.webp"
)
# Run the cwebp command
subprocess.run(
[
"cwebp",
"-q",
"70",
"-metadata",
"icc",
source_path,
"-o",
destination_path,
]
)
print(f"Converted {filename} to {destination_path}")
def main() -> NoReturn:
if len(sys.argv) != 2:
print("Usage: python convert_to_webp.py <folder_path>")
sys.exit(1)
folder_path: str = sys.argv[1]
convert_images_to_webp(folder_path)
if __name__ == "__main__":
main()