Last active
October 16, 2022 15:54
-
-
Save knwng/41c0528f2aad6c6781dc2a4fa773fe87 to your computer and use it in GitHub Desktop.
Convert webp to png and change suffix automatically
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
#!/usr/local/opt/python@3.10/bin/python3 | |
import sys | |
import os | |
from PIL import Image | |
import argparse | |
def webp2png(src_name, dst_name): | |
im = Image.open(src_name).convert('RGB') | |
im.save(dst_name, 'png') | |
def change_suffix(name, suffix): | |
idx = name.rfind('.') | |
if idx == -1: | |
return f'{name}.{suffix}' | |
else: | |
return f'{name[:idx]}.{suffix}' | |
def main(): | |
for im_name in args.files: | |
dst_name = change_suffix(im_name, 'png') | |
webp2png(im_name, dst_name) | |
if args.inplace: | |
os.remove(im_name) | |
print(f'{im_name} converted to {dst_name}, origin file is removed') | |
else: | |
print(f'{im_name} converted to {dst_name}') | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-i', '--inplace', action='store_true', help='inplace') | |
parser.add_argument('files', metavar='FILE', type=str, nargs='+', help='files to be converted') | |
args = parser.parse_args() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment