generated from cotes2020/chirpy-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add script to convert entire folder to webp
- Loading branch information
1 parent
cc60b95
commit b39ba22
Showing
2 changed files
with
61 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
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", 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() |