如何在Python中将BMP图像的整个文件夹转换为PPM?

问题描述 投票:2回答:1

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

我只能转换一张图像,请您帮助将整个图像转换为ppm格式

python-3.x python-imaging-library imaging medical
1个回答
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")
© www.soinside.com 2019 - 2024. All rights reserved.