Skip to content

Instantly share code, notes, and snippets.

@ShadowPower
Last active March 2, 2025 17:24
Show Gist options
  • Select an option

  • Save ShadowPower/53b2369a28c2e9bae3b1ac213e86a54d to your computer and use it in GitHub Desktop.

Select an option

Save ShadowPower/53b2369a28c2e9bae3b1ac213e86a54d to your computer and use it in GitHub Desktop.
# 安装依赖:
# pip install opencv-contrib-python
import numpy as np
import cv2
import os
from cv2.ximgproc import guidedFilter
input_dir = input('请输入存放输入图像的目录:')
output_dir = input('请输入存放处理结果的目录:')
image_exts = ['.jpg', '.jpeg', '.png', '.bmp', '.tif', '.tiff']
for root, dirs, files in os.walk(input_dir):
for file in files:
# 获取文件的绝对路径和后缀
file_path = os.path.join(root, file)
file_ext = os.path.splitext(file)[1].lower()
# 判断文件是否是图片格式
if file_ext in image_exts:
try:
img = cv2.imread(file_path).astype(np.float32)
y = img.copy()
for _ in range(64):
y = cv2.bilateralFilter(y, 5, 8, 8)
for _ in range(4):
y = guidedFilter(img, y, 4, 16)
rel_path = os.path.relpath(file_path, input_dir)
output_path = os.path.join(output_dir, rel_path)
os.makedirs(os.path.dirname(output_path), exist_ok=True)
cv2.imwrite(output_path, y.clip(0, 255).astype(np.uint8))
print(f'成功处理图片:{file_path}')
except Exception as e:
print(f'处理图片出错:{file_path}')
print(f'错误原因:{e}')
opencv-contrib-python
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment