Skip to content

Instantly share code, notes, and snippets.

@khengyun
Created September 24, 2023 21:04
Show Gist options
  • Save khengyun/f9fce201dfb13e676214fc3bf96216f9 to your computer and use it in GitHub Desktop.
Save khengyun/f9fce201dfb13e676214fc3bf96216f9 to your computer and use it in GitHub Desktop.
convert all ppm file to image
import os
from PIL import Image
root_folder = "folder_path"
def convert_ppm_to_jpg(folder_path):
for root, dirs, files in os.walk(folder_path):
for filename in files:
if filename.endswith(".ppm"):
ppm_file_path = os.path.join(root, filename)
with Image.open(ppm_file_path) as img:
jpg_file_path = os.path.splitext(ppm_file_path)[0] + ".jpg"
img.save(jpg_file_path)
os.remove(ppm_file_path)
convert_ppm_to_jpg(root_folder)
print("Conversion and replacement complete.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment