Created
August 27, 2023 19:17
-
-
Save Ari24-cb24/cd2eb62c87a8ab1e97e5eafe669add0d to your computer and use it in GitHub Desktop.
Quick webp converter for web
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
import argparse | |
from PIL import Image | |
import os | |
def dir_path(string): | |
if os.path.isdir(string): | |
return string | |
else: | |
raise NotADirectoryError(string) | |
def convert_to_webp(source, dest): | |
image = Image.open(source) | |
image.save(dest, format="webp") | |
parser = argparse.ArgumentParser( | |
prog='WebP Converter', | |
description='Convert any image to webp', | |
epilog='Made for web and tjc ;)' | |
) | |
group = parser.add_mutually_exclusive_group(required=True) | |
group.add_argument('--file', type=argparse.FileType('r'), nargs='+') | |
group.add_argument('--path', type=dir_path) | |
args = parser.parse_args() | |
files = {*map(lambda a: a.name, args.file)} if args.file is not None else os.listdir(args.path) | |
print(f"Converting {len(files)} files:") | |
for file in files: | |
dest = ".".join(file.split(".")[:-1]) + ".webp" | |
print(f" [ ] '{file}' => '{dest}'", end="") | |
convert_to_webp(file, dest) | |
print(f"\r [x] '{file}' => '{dest}'") | |
print(f"\nFinished converting {len(files)} files!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment