How to open all pictures in python

WBOY
Release: 2024-03-01 13:22:29
forward
693 people have browsed it

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()
Copy after login

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!

Related labels:
source:lsjlt.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template