Chinmaya Urs
Chinmaya Urs

Reputation: 47

How to convert an entire folder of BMP images to PPM in Python?

from PIL import Image 
import glob, os
directory = "your path "
for infile in glob.glob("*.bmp"):
    file, ext = os.path.splitext(infile)
    im = Image.open(infile)
    im.save(file + ".ppm", "PPM")

I was able to convert only one image, can you please help to get entire image converted to ppm format

Upvotes: 2

Views: 1264

Answers (1)

David Slater
David Slater

Reputation: 396

Your code worked for me, so long as I was in the directory of interest. Your code does not use the directory information, so if you are in a different directory, this likely will not find any .ppm files with glob, and so be a no-op. I would modify your code as follows:

from PIL import Image 
import glob, os
target_directory = "/path/to/folder"
for infile in glob.glob(os.path.join(target_directory, "*.bmp")):
    file, ext = os.path.splitext(infile)
    im = Image.open(infile)
    im.save(file + ".ppm", "PPM")

Upvotes: 1

Related Questions