Python去除圖檔水印
- 背景
- 核心代碼
- 思路與代碼解析
- 後話
背景
手裡有一張圖檔,由于水印導緻部分内容被模糊了,于是試想能不能用程式把水印去掉,還原圖檔本真。
核心代碼
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 8 15:39:09 2019
title: 圖檔去水印
@author: Uncle Three
"""
import cv2 #導入PythonCV子產品
import numpy as np #導入數值計算擴充子產品
img=cv2.imread("1.png") #讀圖資料
height,weight,channel=img.shape[0:3] #圖檔的高,寬,和像素通道
thresh=cv2.inRange(img,np.array([170,170,170]),np.array([192,192,192])) ##圖檔二值化處理,把[189, 189, 189]~[192, 192, 192]以外的顔色變成0
scan=np.ones((3,3),np.uint8) #建立膨脹元素
cor=cv2.dilate(thresh,scan,iterations=1) #進行膨脹處理 (将水印三原色覆寫掉)
specular=cv2.inpaint(img,cor,5,flags=cv2.INPAINT_TELEA) #以膨脹結構來修複
cv2.imwrite("result.png",specular) #儲存
思路與代碼解析
先用識别RGB值得軟體識别出水印的RGB值,定位要去除的水印,然後用inRange過濾掉水印的RGB值,然後用水印周邊的進行填補inpaint。
後話
去水印的效果并不好,或者還有很多值得去探究的,如果各位有好的思路,也歡迎留言。