Created
March 29, 2023 05:01
-
-
Save JupyterJones/b427eefa0e3d13be41b802c2a5a1f448 to your computer and use it in GitHub Desktop.
Convert Webp to jpg
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
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