Python OpenCV Watermark Watermark

Source: Internet
Author: User

The request received is to match the watermark on a graph and replace the original watermark with a new one.

The first one to install a library file code is as follows:

# Coding=utf-8import Cv2import NumPy as np# expansion algorithm kernel_dilate_kernel = Np.array ([[0, 0, 1, 0, 0],                           [0, 0, 1, 0, 0], [1, 1, 1, 1, 1], [0, 0, 1, 0, 0],    [0, 0, 1, 0, 0]], dtype=np.uint8) class Watermarkremover (object): "" "Removes the watermark in the picture (remove Watermark)" " def __init__ (self, verbose=true): self.verbose = verbose self.watermark_template_gray_img = None sel f.watermark_template_mask_img = None Self.watermark_template_h = 0 Self.watermark_template_w = 0 Self . watermark_start_x = 0 self.watermark_start_y = 0 def load_watermark_template (self, watermark_template_filename) : "" Loads the watermark template so that the watermark is removed later:p Aram Watermark_template_filename:: Return: "" "self.        Generate_template_gray_and_mask (Watermark_template_filename) def dilate (Self, img): "" to inflate a picture :p Aram IMG:: RetuRN: "" "dilated = Cv2.dilate (img, _dilate_kernel) return dilated def Generate_template_gray_and_ma        SK (Self, Watermark_template_filename): "" "handles watermark template, generates corresponding retrieval bitmap and mask bitmap retrieves bitmap as processed grayscale, removes non-text part  :p Aram Watermark_template_filename: watermark Template Picture file name: return:x1, y1, x2, y2 "" "# Watermark Template Original img = Cv2.imread (watermark_template_filename) # Grayscale, mask graph Gray = Cv2.cvtcolor (img, Cv2. Color_bgr2gray) _, mask = Cv2.threshold (gray, 0, 255, Cv2. Thresh_tozero + CV2. Thresh_otsu) _, Mask = Cv2.threshold (mask, 127, 255, Cv2. Thresh_binary) mask = self.dilate # causes the mask to swell a lap so that the edges are not repaired #mask = Self.dilate (mask) # causes the mask to swell one lap to avoid leaving the edge No fix # watermark template artwork to go unless the text part of IMG = Cv2.bitwise_and (IMG, IMG, mask=mask) # requires three channel mask to be used after the map is repaired = CV2.CV TColor (Mask, Cv2. COLOR_GRAY2BGR) self.watermark_template_gray_img = Gray self.watermark_template_mask_img = Mask self. WatermarK_template_h = img.shape[0] Self.watermark_template_w = img.shape[1] # cv2.imwrite (' Watermark-template-gray.  JPG ', gray) # cv2.imwrite (' watermark-template-mask.jpg ', mask) return gray, Mask def find_watermark (self, FileName): "" To find the watermark location from the original image:p Aram FileName:: return:x1, Y1, x2, y2 "" "# Load T He images in gray scale gray_img = cv2.imread (filename, 0) return Self.find_watermark_from_gray (gray_img, SE        LF.WATERMARK_TEMPLATE_GRAY_IMG) def find_watermark_from_gray (self, gray_img, watermark_template_gray_img): "" " Looking for the watermark position from the gray image of the original map:p Aram Gray_img: Grayscale image of the original:p Aram Watermark_template_gray_img: Grayscale image of the watermark template: return:x1 , y1, x2, y2 "" "# Load the images in gray scale method = Cv2.        Tm_ccoeff # Apply Template Matching res = cv2.matchtemplate (gray_img, watermark_template_gray_img, method)  Min_val, Max_val, min_loc, Max_loc = Cv2.minmaxloc (res)      # If the method is Tm_sqdiff or tm_sqdiff_normed, take minimum If method in [Cv2. Tm_sqdiff, Cv2. Tm_sqdiff_normed]: x, y = Min_loc else:x, y = max_loc return x, y, x + self.watermark _template_w, y + self.watermark_template_h def remove_watermark_raw (Self, IMG, watermark_template_gray_img, Watermark_ TEMPLATE_MASK_IMG): "" "Removes the watermark from the image:p Aram img: Watermark image to be removed bitmap:p Aram watermark_template_gray_img: Watermark Mode        A grayscale picture bitmap of the board, used to determine the location of the watermark:p Aram watermark_template_mask_img: Mask picture bitmap of the watermark template, used to fix the original picture: return: Image Bitmap "" "after watermark removal # Find the watermark location Img_gray = Cv2.cvtcolor (img, Cv2. Color_bgr2gray) x1, y1, x2, y2 = Self.find_watermark_from_gray (Img_gray, watermark_template_gray_img) SELF.W        atermark_start_x = x1 self.watermark_start_y = y1 # watermark position of the original artwork mask mask = Np.zeros (Img.shape, Np.uint8) # watermark_template_mask_img = Cv2.cvtcolor (watermark_template_gray_img, Cv2.      COLOR_GRAY2BGR)  # Mask[y1:y1 + self.watermark_template_h, x1:x1 + self.watermark_template_w] = watermark_template_mask_img mask[y 1:y2, x1:x2] = watermark_template_mask_img mask = Cv2.cvtcolor (Mask, Cv2. Color_bgr2gray) # with a mask for picture repair, use the Telea algorithm DST = Cv2.inpaint (IMG, Mask, 4, Cv2. Inpaint_telea) # cv2.imwrite (' dst.jpg ', DST) return DST def remove_watermark (self, filename, Output_filen        Ame=none): "" "Removes the watermark from the image:p Aram FileName: The watermark image file name to be removed:p Aram output_filename: The output file name after removing the watermark image : return: Image after removal watermark "" "# Read original IMG = cv2.imread (filename) DST = Self.remove_watermark_raw (IMG, self.watermark_template_gray_img, self . watermark_template_mask_img) If Output_filename is not NONE:CV 2.imwrite (Output_filename, DST) return DST

Note that the above code adds these two sentences to show the location of the original watermark.

Go to the watermark code as follows:

from nowatermark import WatermarkRemoverpath = ‘E:/sample/‘watermark_template_filename = path + ‘watermark.png‘remover = WatermarkRemover()remover.load_watermark_template(watermark_template_filename)remover.remove_watermark(path + ‘20180516144931.png‘, path + ‘20180516144932.png‘)print(remover.watermark_start_x)print(remover.watermark_start_y)

Here the output of the two values refers to the location of the watermark in the original image

Add watermark code as follows:

import cv2import numpy as nppath = ‘E:/sample/‘matimage = cv2.imread(path + ‘20180516144932.png‘)#matimagenew = np.zeros((matimage.shape[0],matimage.shape[1],3))matimagenew = matimage-matimagewatermark_template_filename = path + ‘watermark.png‘matlogo = cv2.imread(watermark_template_filename)matimagenew[359:359+matlogo.shape[0],453:453+matlogo.shape[1]] = matlogoimagenew = cv2.addWeighted(matimage,1,matimagenew,1,1)savepath = path + ‘20180516144933.png‘cv2.imwrite(savepath,imagenew)

359 of which is the position of the watermark in the original coordinates 453 is the horizontal axis

Python OpenCV Watermark Watermark

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.