Home  >  Article  >  Backend Development  >  How to open all pictures in python

How to open all pictures in python

WBOY
WBOYforward
2024-03-01 13:22:29695browse

How to open all pictures in python

You can use the os module to traverse all pictures in a specified folder, and use the PIL library to open and display pictures. Here is a sample code:

import os
from PIL import Image

# 指定图片文件夹路径
image_folder = "/path/to/image/folder"

# 遍历文件夹中的所有文件
for filename in os.listdir(image_folder):
# 检查文件是否为图片文件
if filename.endswith(".jpg") or filename.endswith(".png"):
# 构造图片的完整路径
image_path = os.path.join(image_folder, filename)

# 打开图片
image = Image.open(image_path)

# 显示图片
image.show()

Please replace /path/to/image/folder with your actual image folder path. The above code will open all the images in the specified folder and display them on the screen in sequence.

The above is the detailed content of How to open all pictures in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lsjlt.com. If there is any infringement, please contact admin@php.cn delete