Skip to content

Instantly share code, notes, and snippets.

@ProGamerGov
Created February 12, 2023 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ProGamerGov/c49d872b86fffd37be9f1fd118d89f97 to your computer and use it in GitHub Desktop.
Save ProGamerGov/c49d872b86fffd37be9f1fd118d89f97 to your computer and use it in GitHub Desktop.
Convert all '.webp' images in a dataset to '.png'
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