批量锐化图片

import cv2
import os
import numpy as np


def cv2_filter2d(img_path):
    src = cv2.imread(img_path)
    kernel = np.array([[0, -1, 0],
                       [-1, 5, -1],
                       [0, -1, 0]])
    dst = cv2.filter2D(src, -1, kernel)
    # cv2.imshow('original', src)
    # cv2.imshow('dst', dst)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()
    return dst


def read_path(file_pathname):
    for filename in os.listdir(file_pathname):
        print(filename)
        # img = cv2.imread(file_pathname+'/'+filename)
        # (下面第一行是将RGB转成单通道灰度图,第二步是将单通道灰度图转成3通道灰度图)
        # img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        # image_np=cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
        image_np = cv2_filter2d(file_pathname + '/' + filename)
        cv2.imwrite('JPEGImages' + "/" + filename, image_np)


read_path("JPEGImages1")

你可能感兴趣的:(python,opencv,计算机视觉,python)