Created
February 12, 2023 21:09
-
-
Save ProGamerGov/c49d872b86fffd37be9f1fd118d89f97 to your computer and use it in GitHub Desktop.
Convert all '.webp' images in a dataset to '.png'
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
import os | |
from PIL import Image | |
def convert_webp_to_png(directory: str, delete_old_webp_images: bool = False): | |
for root, dirs, files in os.walk(directory): | |
for file in files: | |
if file.endswith(".webp"): | |
filepath = os.path.join(root, file) | |
img = Image.open(filepath) | |
new_filepath = os.path.splitext(filepath)[0] + ".png" | |
img.save(new_filepath, "png", quality=100) | |
if delete_old_webp_images: | |
os.remove(filepath) | |
convert_webp_to_png("/path/to/directory") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment