Python创建一个图像目录
我目前正在做一个项目,到目前为止,我已经生成了一个图像文件夹(png格式),在那里我需要迭代每个图像,并使用PIL对其进行一些操作 通过手动将文件路径链接到脚本中,我使操作正常工作。 为了遍历每个图像,我尝试使用以下方法Python创建一个图像目录,python,glob,pathlib,Python,Glob,Pathlib,我目前正在做一个项目,到目前为止,我已经生成了一个图像文件夹(png格式),在那里我需要迭代每个图像,并使用PIL对其进行一些操作 通过手动将文件路径链接到脚本中,我使操作正常工作。 为了遍历每个图像,我尝试使用以下方法 frames = glob.glob("*.png") 但这会产生一个文件名列表作为字符串 PIL需要一个文件路径来加载映像并进一步使用 filename = input("file path:") image = Image.open(filename) callimage
frames = glob.glob("*.png")
但这会产生一个文件名列表作为字符串
PIL需要一个文件路径来加载映像并进一步使用
filename = input("file path:")
image = Image.open(filename)
callimage = image.load()
如何转换glob.glob列表中的字符串并将其用作Image.open方法的参数
谢谢你的反馈
如果这与python 3.6.1有任何关联的话,我将使用python 3.6.1 带有
os
软件包的解决方案:
import os
source_path = "my_path"
image_files = [os.path.join(base_path, f) for f in files for base_path, _, files in os.walk(source_path) if f.endswith(".png")]
for filepath in image_files:
callimage = Image.open(filepath).load()
# ...
使用glob的解决方案
:
import glob
source_path = "my_path"
image_files = [source_path + '/' + f for f in glob.glob('*.png')]
for filepath in image_files:
callimage = Image.open(filepath).load()
# ...