Created
April 10, 2023 13:03
-
-
Save SnailShea/6b9f41157cb9ff6b417f7841e9b73aaa to your computer and use it in GitHub Desktop.
Convert WEBP files to PNG quickly with Python
This file contains hidden or 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/bin/python3 | |
# Usage: webp_to_png <filename> | |
def arg_chk(args): | |
if len(args) != 2: | |
print("Script takes exactly one argument") | |
quit() | |
def exist_chk(img): | |
from os.path import exists | |
if not exists(img): | |
print("File not found") | |
quit() | |
def img_ext(img): | |
from os.path import splitext | |
ext = splitext(img)[1] | |
if ext != ".webp": | |
print("Only .WEBP files are accepted") | |
quit() | |
def ext_to_png(img): | |
from os.path import splitext | |
path, ext = splitext(img) | |
new_img = f"{path}.png" | |
return new_img | |
def img_conv(img): | |
from PIL import Image | |
png_file = ext_to_png(img) | |
png = Image.open(img).convert("RGB") | |
png.save(png_file, "png") | |
print(f"Saved to {png_file}") | |
if __name__ == "__main__": | |
from sys import argv | |
arg_chk(argv) | |
exist_chk(argv[1]) | |
img_ext(argv[1]) | |
img_conv(argv[1]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment