python opencv 去水印实现

主要利用了 opencv 的 inpaint 方法,实现类似蒙版的效果
http://docs.opencv.org/master/df/d3d/tutorial_py_inpainting.html#gsc.tab=0

效果:注意右下角&左上角
原图

处理后:

'''
string to array
'''
def get_opencv_img_from_string(data):
    arr = np.array(bytearray(data), dtype=np.uint8)
    img = cv2.imdecode(arr, -1)
    return img

'''
array to string
'''
def get_buffer_from_bytes_array(data):
    arr = cv2.imencode(".jpg", data)[1].tostring()
    return arr


def watermark_clean(data, platform):
    img = get_opencv_img_from_string(data)
    height = img.shape[0]
    width = img.shape[1]
    rects = ((width - 169, height - 22, width, height),(1, 1, 164, 21)) #水印区域
    cv2.imshow('a', img)
    mask = np.zeros(img.shape[:2], np.uint8)
    for rect in rects:
        x1, y1, x2, y2 = rect
        cv2.rectangle(mask, (x1, y1), (x2, y2), (255, 255, 255), -1)
        img = cv2.inpaint(img, mask, 10.0, cv2.INPAINT_TELEA) #蒙版
    return get_buffer_from_bytes_array(img)


def show_img(img_arr):
    cv2.imshow('test', img_arr)
    cv.WaitKey(0)

发表评论

电子邮件地址不会被公开。 必填项已用*标注