python opencv2 去小红书水印

去除文件夹内所有.jPg结尾的小红书水印
python opencv2 去小红书水印_第1张图片


直接上源码

import os
import cv2
import numpy as np

def file_name(file_dir):
    for root, dirs, files in os.walk(file_dir):
        return files

ba=file_name(os.getcwd())
for file in ba:
    if file[-3:] == "jpg":
        print(file)  # 替换的文件名
        # 加载原始图片
        image = cv2.imread(file)  # image :是返回提取到的图片的值
        size = image.shape
        w = size[1]  # 宽度
        h = size[0]  # 高度
        print(w, h)
        cropped_image = image[h - 110:h - 20, w - 300:w - 20]  #
        lower = np.array([200, 200, 200])  # 可以调整
        upper = np.array([255, 255, 255])
        thresh = cv2.inRange(cropped_image, lower, upper)
        k = cv2.getStructuringElement(cv2.MORPH_RECT, (4, 4))
        binary = cv2.dilate(thresh, k)
        size2 = binary.shape
        w2 = size2[1]  # 宽度
        h2 = size2[0]  # 高度
        print(w2, h2)
        # s2=binary[0:h2,0:w2]
        gray0 = np.zeros((h, w), dtype=np.uint8)
        for i in range(h2):  # 获取高度数值
            k = 0
            for j in binary[i]:  # 高度行的数值
                if j == 255:
                    gray0[h - 110 + i, w - 300 + k] = 255
                k = k + 1
        # image 是原始图像
        # gray0 是底图
        dst = cv2.inpaint(image, gray0, 0.1, cv2.INPAINT_TELEA)
        cropped_dst = dst[h - 110:h - 20, w - 300:w - 20]  #
        cv2.imshow("jpg", cropped_dst)
        cv2.waitKey(0)
        cv2.imwrite(str(file), dst)

你可能感兴趣的:(python)