
Python读取文件夹中多个图片的方法:使用os模块、使用glob模块、使用Pathlib模块。
其中,使用os模块是最基础和直观的方法。os模块提供了与操作系统进行交互的接口,可以列出文件夹中的所有文件,并通过循环读取每个图片文件。以下是详细描述。
使用os模块读取图片:首先导入os模块,然后使用os.listdir()函数列出文件夹中的所有文件。接着,通过文件扩展名过滤出图片文件,并使用PIL(Python Imaging Library)或OpenCV库读取这些图片。以下是示例代码:
import os
from PIL import Image
def read_images_from_folder(folder_path):
image_list = []
for filename in os.listdir(folder_path):
if filename.endswith(".jpg") or filename.endswith(".png"):
img = Image.open(os.path.join(folder_path, filename))
if img is not None:
image_list.append(img)
return image_list
示例使用
folder_path = 'path/to/your/folder'
images = read_images_from_folder(folder_path)
for img in images:
img.show()
一、使用os模块读取图片
1、基本操作
使用os模块的基本方法如下,首先需要导入os模块,并指定文件夹路径,然后列出该文件夹中的所有文件。
import os
folder_path = 'path/to/your/folder'
files = os.listdir(folder_path)
print(files)
这段代码会打印出文件夹中的所有文件名。接着,我们需要过滤出其中的图片文件。
2、过滤图片文件
我们可以使用文件扩展名进行过滤,例如只读取.jpg和.png格式的图片。
image_files = [f for f in files if f.endswith(('.jpg', '.png'))]
print(image_files)
这样,我们就得到了文件夹中所有图片文件的列表。
3、读取图片文件
接下来,我们使用PIL库的Image模块来读取图片文件。
from PIL import Image
def read_images(folder_path):
image_list = []
for filename in os.listdir(folder_path):
if filename.endswith(".jpg") or filename.endswith(".png"):
img = Image.open(os.path.join(folder_path, filename))
if img is not None:
image_list.append(img)
return image_list
示例使用
folder_path = 'path/to/your/folder'
images = read_images(folder_path)
for img in images:
img.show()
这段代码会读取文件夹中的所有图片文件,并将它们存储在一个列表中。然后,我们可以对这些图片进行处理,例如显示它们。
二、使用glob模块读取图片
1、导入glob模块
glob模块提供了一种文件模式匹配的方法,可以方便地列出符合特定模式的文件。
import glob
from PIL import Image
folder_path = 'path/to/your/folder'
image_files = glob.glob(folder_path + '/*.jpg') + glob.glob(folder_path + '/*.png')
print(image_files)
这段代码会列出文件夹中所有.jpg和.png格式的图片文件。
2、读取图片文件
接下来,我们使用PIL库的Image模块来读取图片文件。
def read_images_with_glob(folder_path):
image_files = glob.glob(folder_path + '/*.jpg') + glob.glob(folder_path + '/*.png')
image_list = []
for filename in image_files:
img = Image.open(filename)
if img is not None:
image_list.append(img)
return image_list
示例使用
folder_path = 'path/to/your/folder'
images = read_images_with_glob(folder_path)
for img in images:
img.show()
这段代码会读取文件夹中的所有图片文件,并将它们存储在一个列表中。然后,我们可以对这些图片进行处理,例如显示它们。
三、使用Pathlib模块读取图片
1、导入Pathlib模块
Pathlib模块提供了一种面向对象的方法来处理文件和文件夹路径。相比os模块,Pathlib的代码更简洁和易读。
from pathlib import Path
from PIL import Image
folder_path = Path('path/to/your/folder')
image_files = list(folder_path.glob('*.jpg')) + list(folder_path.glob('*.png'))
print(image_files)
这段代码会列出文件夹中所有.jpg和.png格式的图片文件。
2、读取图片文件
接下来,我们使用PIL库的Image模块来读取图片文件。
def read_images_with_pathlib(folder_path):
folder_path = Path(folder_path)
image_files = list(folder_path.glob('*.jpg')) + list(folder_path.glob('*.png'))
image_list = []
for filename in image_files:
img = Image.open(filename)
if img is not None:
image_list.append(img)
return image_list
示例使用
folder_path = 'path/to/your/folder'
images = read_images_with_pathlib(folder_path)
for img in images:
img.show()
这段代码会读取文件夹中的所有图片文件,并将它们存储在一个列表中。然后,我们可以对这些图片进行处理,例如显示它们。
四、其他常见问题和解决方法
1、处理子文件夹中的图片
有时候,图片文件可能分布在多个子文件夹中。我们可以使用递归的方法来处理这种情况。
def read_images_recursively(folder_path):
folder_path = Path(folder_path)
image_files = list(folder_path.rglob('*.jpg')) + list(folder_path.rglob('*.png'))
image_list = []
for filename in image_files:
img = Image.open(filename)
if img is not None:
image_list.append(img)
return image_list
示例使用
folder_path = 'path/to/your/folder'
images = read_images_recursively(folder_path)
for img in images:
img.show()
这段代码会递归读取文件夹及其子文件夹中的所有图片文件。
2、处理不同格式的图片
有时候,图片文件可能有多种格式。我们可以通过扩展名列表来处理这种情况。
def read_images_with_formats(folder_path, formats):
folder_path = Path(folder_path)
image_files = []
for fmt in formats:
image_files += list(folder_path.glob(f'*.{fmt}'))
image_list = []
for filename in image_files:
img = Image.open(filename)
if img is not None:
image_list.append(img)
return image_list
示例使用
folder_path = 'path/to/your/folder'
formats = ['jpg', 'png', 'bmp']
images = read_images_with_formats(folder_path, formats)
for img in images:
img.show()
这段代码会读取文件夹中所有指定格式的图片文件。
五、常用库的介绍
在处理图片文件时,我们通常会使用一些第三方库,例如PIL和OpenCV。下面是这些库的简要介绍。
1、PIL库
PIL(Python Imaging Library)是一个强大的图像处理库,支持多种图像格式。使用PIL库可以方便地读取、修改和保存图像文件。
from PIL import Image
img = Image.open('path/to/your/image.jpg')
img.show()
2、OpenCV库
OpenCV(Open Source Computer Vision Library)是一个开源的计算机视觉和机器学习库,支持多种图像和视频处理功能。使用OpenCV库可以方便地读取、修改和保存图像文件。
import cv2
img = cv2.imread('path/to/your/image.jpg')
cv2.imshow('Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
六、综合示例
最后,我们通过一个综合示例来展示如何使用上述方法读取文件夹中的多个图片文件,并对它们进行处理。
import os
import glob
from pathlib import Path
from PIL import Image
import cv2
def read_images_os(folder_path):
image_list = []
for filename in os.listdir(folder_path):
if filename.endswith(".jpg") or filename.endswith(".png"):
img = Image.open(os.path.join(folder_path, filename))
if img is not None:
image_list.append(img)
return image_list
def read_images_glob(folder_path):
image_files = glob.glob(folder_path + '/*.jpg') + glob.glob(folder_path + '/*.png')
image_list = []
for filename in image_files:
img = Image.open(filename)
if img is not None:
image_list.append(img)
return image_list
def read_images_pathlib(folder_path):
folder_path = Path(folder_path)
image_files = list(folder_path.glob('*.jpg')) + list(folder_path.glob('*.png'))
image_list = []
for filename in image_files:
img = Image.open(filename)
if img is not None:
image_list.append(img)
return image_list
示例使用
folder_path = 'path/to/your/folder'
images_os = read_images_os(folder_path)
images_glob = read_images_glob(folder_path)
images_pathlib = read_images_pathlib(folder_path)
显示图片
for img in images_os:
img.show()
for img in images_glob:
img.show()
for img in images_pathlib:
img.show()
这个综合示例展示了如何使用os模块、glob模块和Pathlib模块读取文件夹中的多个图片文件,并对它们进行处理。
相关问答FAQs:
1. 如何使用Python从文件夹中读取多个图片?
你可以使用Python中的os和PIL库来实现从文件夹中读取多个图片的操作。首先,使用os库中的listdir函数获取文件夹中的所有文件名。然后,使用PIL库中的Image模块来读取每个文件并进行处理。
2. 在Python中如何遍历文件夹中的所有图片文件?
要遍历文件夹中的所有图片文件,你可以使用os库中的walk函数。这个函数可以递归地遍历文件夹中的所有子文件夹和文件。然后,你可以使用PIL库中的Image模块来判断文件是否是图片文件,并进行处理。
3. 如何将从文件夹中读取的多个图片保存到一个列表中?
你可以使用一个空列表来保存从文件夹中读取的多个图片。首先,使用os库中的listdir函数获取文件夹中的所有文件名。然后,使用一个循环来遍历每个文件,使用PIL库中的Image模块来读取图片,并将它们添加到列表中。最后,你可以使用这个列表进行后续的操作。
文章包含AI辅助创作,作者:Edit2,如若转载,请注明出处:https://docs.pingcode.com/baike/932879