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

时间:2019-03-19 14:42:42

标签: python-3.x python-imaging-library imaging medical

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

1 个答案:

答案 0 :(得分:1)

只要我在感兴趣的目录中,您的代码就可以为我工作。您的代码未使用directory信息,因此,如果您在其他目录中,则可能找不到带有.ppm的任何glob文件,因此是空操作。我将修改您的代码,如下所示:

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")