python读取文件夹中图片并集中显示
时间: 2023-10-11 12:12:01 浏览: 109
你可以使用Python的Pillow库来读取文件夹中的图片并将它们集中显示。以下是一个简单的示例代码:
```python
from PIL import Image
import os
# 设置文件夹路径
folder_path = 'your_folder_path'
# 获取文件夹中所有图片的文件名
image_files = [f for f in os.listdir(folder_path) if f.endswith('.jpg') or f.endswith('.png')]
# 创建一个空白的画布
canvas = Image.new('RGB', (800, 800), 'white')
# 设置图片的起始位置
x = 0
y = 0
# 遍历所有图片并将它们粘贴到画布上
for image_file in image_files:
# 打开图片并调整大小以适应画布
image = Image.open(os.path.join(folder_path, image_file))
image.thumbnail((400, 400))
# 将图片粘贴到画布上
canvas.paste(image, (x, y))
# 更新图片的起始位置
if x + image.width < canvas.width:
x = x + image.width
else:
x = 0
y = y + image.height
# 显示画布
canvas.show()
```
请注意,此代码假定所有文件夹中的文件都是图片文件(.jpg或.png)。如果有其他类型的文件,你需要调整代码来排除它们。
阅读全文
相关推荐
















