import cv2
import numpy as np
from tqdm import tqdm

def blur(image, degree=12, angle=45):
    image = np.array(image)
    # 这里生成任意角度的运动模糊kernel的矩阵， degree越大，模糊程度越高
    M = cv2.getRotationMatrix2D((degree / 2, degree / 2), angle, 1)
    motion_blur_kernel = np.diag(np.ones(degree))
    motion_blur_kernel = cv2.warpAffine(motion_blur_kernel, M, (degree, degree))
    motion_blur_kernel = motion_blur_kernel / degree
    blurred = cv2.filter2D(image, -1, motion_blur_kernel)
    # convert to uint8
    cv2.normalize(blurred, blurred, 0, 255, cv2.NORM_MINMAX)
    blurred = np.array(blurred, dtype=np.uint8)
    return blurred

for i in tqdm(range(1, 546)):
    img = cv2.imread('./pro_data/'+str(i)+'.jpg')
    # img_ = cv2.GaussianBlur(img, (15, 15), 3)
    cv2.imwrite('./pro_data_blurred/'+str(i)+'.jpg', blur(img, 50))
cv2.waitKey()