first commit
After Width: | Height: | Size: 308 KiB |
After Width: | Height: | Size: 1.3 MiB |
After Width: | Height: | Size: 79 KiB |
After Width: | Height: | Size: 22 KiB |
After Width: | Height: | Size: 28 KiB |
After Width: | Height: | Size: 228 KiB |
After Width: | Height: | Size: 433 KiB |
After Width: | Height: | Size: 40 KiB |
After Width: | Height: | Size: 18 KiB |
After Width: | Height: | Size: 43 KiB |
After Width: | Height: | Size: 347 KiB |
After Width: | Height: | Size: 392 KiB |
After Width: | Height: | Size: 244 KiB |
After Width: | Height: | Size: 284 KiB |
After Width: | Height: | Size: 389 KiB |
After Width: | Height: | Size: 308 KiB |
After Width: | Height: | Size: 385 KiB |
After Width: | Height: | Size: 262 KiB |
After Width: | Height: | Size: 262 KiB |
After Width: | Height: | Size: 294 KiB |
|
@ -0,0 +1,63 @@
|
|||
import cv2
|
||||
from PIL import Image
|
||||
import numpy as np
|
||||
import os
|
||||
|
||||
|
||||
def calculate_clarity_score(image_path):
|
||||
"""
|
||||
计算图片的清晰度评分
|
||||
"""
|
||||
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
|
||||
if image is None:
|
||||
raise ValueError(f"无法读取图片:{image_path}")
|
||||
return cv2.Laplacian(image, cv2.CV_64F).var()
|
||||
|
||||
|
||||
def process_images(input_folder, output_folder,clarity_score_threshold):
|
||||
if not os.path.exists(input_folder):
|
||||
print(f"输入文件夹不存在:{input_folder}")
|
||||
return
|
||||
|
||||
for root, dirs, files in os.walk(input_folder):
|
||||
print(f'——————正在处理文件夹:{root}, 图片数量:{len(files)}——————')
|
||||
i = 0
|
||||
for filename in files:
|
||||
if not filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif', '.jfif')):
|
||||
continue
|
||||
file_path = os.path.join(root, filename)
|
||||
# print(f'file_path is {file_path}')
|
||||
i+=1
|
||||
try:
|
||||
# 清晰度检测
|
||||
clarity_score = calculate_clarity_score(file_path)
|
||||
if clarity_score > clarity_score_threshold:
|
||||
# 读取图片
|
||||
with Image.open(file_path) as img:
|
||||
# resize
|
||||
img = img.resize((512, 512))
|
||||
|
||||
# output_subfolder
|
||||
relative_path = os.path.relpath(root, input_folder)
|
||||
output_subfolder = os.path.join(output_folder, relative_path)
|
||||
# print(f'root: {root}, input_folder: {input_folder}, relative_path: {relative_path}, output_subfolder: {output_subfolder}')
|
||||
|
||||
|
||||
if not os.path.exists(output_subfolder):
|
||||
os.makedirs(output_subfolder)
|
||||
output_path = os.path.join(output_subfolder, os.path.splitext(filename)[0] + ".png")
|
||||
img.save(output_path, format="PNG")
|
||||
print(f"处理并保存图片{i}:{file_path} -> {output_path}")
|
||||
else:
|
||||
print(f"********图片 {file_path} 清晰度评分 {clarity_score} 不满足要求,跳过********")
|
||||
except Exception as e:
|
||||
print(f"********处理图片 {file_path} 时出错:{e}********")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
input_folder = 'PMN_WS/data_process/datasets/source' # 输入文件夹路径
|
||||
output_folder = 'PMN_WS/data_process/datasets/target' # 输出文件夹路径
|
||||
clarity_score_threshold =50
|
||||
process_images(input_folder, output_folder,clarity_score_threshold)
|
||||
|
||||
|