Skip to content

Instantly share code, notes, and snippets.

@khengyun
Last active September 24, 2023 21:04
Show Gist options
  • Save khengyun/92d321b301df4ddc60c7bc7b6dbbce42 to your computer and use it in GitHub Desktop.
Save khengyun/92d321b301df4ddc60c7bc7b6dbbce42 to your computer and use it in GitHub Desktop.
convert all image in a folder to ppm format
import os
from PIL import Image
root_folder = "folder_path"
def convert_image_to_ppm(folder_path):
for root, dirs, files in os.walk(folder_path):
for filename in files:
if filename.endswith((".png", ".jpg")):
image_file_path = os.path.join(root, filename)
with Image.open(image_file_path) as img:
ppm_file_path = os.path.splitext(image_file_path)[0] + ".ppm"
img.save(ppm_file_path)
os.remove(image_file_path)
convert_image_to_ppm(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