Skip to content

Instantly share code, notes, and snippets.

@JupyterJones
Created March 29, 2023 05:01
Show Gist options
  • Save JupyterJones/b427eefa0e3d13be41b802c2a5a1f448 to your computer and use it in GitHub Desktop.
Save JupyterJones/b427eefa0e3d13be41b802c2a5a1f448 to your computer and use it in GitHub Desktop.
Convert Webp to jpg
from PIL import Image
import os
# Define the directory to use
directory = "testfiles/"
# Iterate over the files in the directory
for filename in os.listdir(directory):
filepath = os.path.join(directory, filename)
if filename.endswith(".webp"):
# Open the WebP image file
webp_image = Image.open(filepath)
# Convert to JPG format and save
jpg_filename = os.path.join(directory, filename[:-5] + ".jpg")
webp_image.convert("RGB").save(jpg_filename, "JPEG")
print(f"{filename} converted to JPG format.")
# Delete the original WebP file
os.remove(filepath)
print(f"{filename} deleted.")
else:
print(f"{filename} is not a WebP file and will not be converted.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment