32 lines
901 B
Python
32 lines
901 B
Python
# -*- coding:utf-8 -*-
|
|
# @Author len
|
|
# @Create 2023/10/19 10:00
|
|
|
|
# 使用opencv灰度化
|
|
import cv2
|
|
import os
|
|
|
|
# 图片所在文件夹路径
|
|
folder_path = r'data'
|
|
# 保存灰度图像的目标文件夹路径
|
|
save_folder_path = r'data'
|
|
|
|
# 获取文件夹中所有图像文件的路径
|
|
image_paths = [os.path.join(folder_path, filename) for filename in os.listdir(folder_path) if filename.endswith(('.jpg', '.jpeg', '.png'))]
|
|
|
|
# 批量灰度化处理并保存
|
|
for image_path in image_paths:
|
|
# 读取图像
|
|
print(image_path)
|
|
image = cv2.imread(image_path)
|
|
|
|
# 将图像转换为灰度图像
|
|
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
|
|
# 确定保存的文件名
|
|
filename = os.path.basename(image_path)
|
|
save_filename = 'gray_' + filename
|
|
save_path = os.path.join(save_folder_path, save_filename)
|
|
|
|
# 保存灰度图像
|
|
cv2.imwrite(save_path, gray_image) |