Last active
September 9, 2022 03:50
-
-
Save Lokno/2078986c3fc25bb7f80374be9a99b611 to your computer and use it in GitHub Desktop.
Python 3 script that replaces every webp on your desktop with a PNG. Requires Google's dwebp decoder (https://developers.google.com/speed/webp/docs/dwebp)
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
# Webp-Begone | |
# | |
# Iterates over all the files ending in ".webp" on the Desktop and converts | |
# them to PNG files using the Google utiltiy dwebp | |
# Alteratively a different directory path can be specified at the cmdline | |
# Install dwebp and add it to your path | |
# https://developers.google.com/speed/webp/docs/dwebp | |
# https://storage.googleapis.com/downloads.webmproject.org/releases/webp/index.html | |
import os | |
from sys import argv | |
from shutil import which | |
if which('dwebp') is None: | |
print('The utility dwebp cannot be found.') | |
print('Visit: https://developers.google.com/speed/webp/docs/dwebp') | |
input("Press Enter to continue...") | |
os.exit(-1) | |
dir_path = None | |
if len(argv) > 1: | |
if os.path.exists(argv[1]) and os.path.isdir(argv[1]): | |
dir_path = argv[1] | |
else: | |
print(f'ERROR: Not a valid directory') | |
input("Press Enter to continue...") | |
os.exit(-1) | |
if dir_path is None: | |
if 'HOME' in os.environ: | |
dir_path = os.environ['HOME'] | |
if 'HOMEPATH' in os.environ and 'HOMEDRIVE' in os.environ: | |
dir_path = os.environ['HOMEDRIVE'] + os.environ['HOMEPATH'] | |
if dir_path is not None: | |
dir_path = os.path.join(dir_path, 'Desktop') | |
if os.path.exists(dir_path): | |
os.chdir(dir_path) | |
for filename in os.listdir('.'): | |
if os.path.isfile(filename): | |
root,ext = os.path.splitext(filename) | |
if ext == '.webp': | |
os.system(f'dwebp {filename:s} -o {root:s}.png') | |
os.system(f'del {filename:s}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment