-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgray.py
More file actions
32 lines (24 loc) · 1023 Bytes
/
Copy pathgray.py
File metadata and controls
32 lines (24 loc) · 1023 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#给我一个程序对文件夹内的图片变为灰度图像替换原文件:
import os
import cv2
def convert_to_gray(folder_path):
# 遍历文件夹中的所有文件
for filename in os.listdir(folder_path):
file_path = os.path.join(folder_path, filename)
# 检查文件是否为图片文件
if not os.path.isfile(file_path) or not any(
file_path.lower().endswith(ext) for ext in ['.jpg', '.jpeg', '.png']):
continue
# 读取图像文件
image = cv2.imread(file_path, cv2.IMREAD_GRAYSCALE)
# 将图像转换为灰度图像
if image is not None:
# 保存灰度图像,覆盖原文件
cv2.imwrite(file_path, image)
print(f"Converted {filename} to grayscale.")
else:
print(f"Failed to read {filename}.")
# 指定要转换的文件夹路径
folder_path = "E:\Python_Project\Color2Embed-main\data\mydata\\20001"
# 调用函数进行转换
convert_to_gray(folder_path)