Wechat applet to watermark code analysis!

I. Introduction

When we browse WeChat Moments or other social platforms, we often encounter some interesting videos or photos, but there are hateful watermarks on these contents. These watermarks not only affect our perception, but also lower our image on social media. Therefore, the WeChat watermark removal applet came into being. Allows us to easily remove watermarks from pictures and videos.

2. Function realization

1. Download the watermarked image or video

First, we need to get the pictures or videos that need to be watermarked from WeChat or other social media. This can be achieved through some third party library or API. For example:

```python

import requests

from bs4 import BeautifulSoup

def get_video(url):

headers = {

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}

html = requests.get(url, headers=headers).text

soup = BeautifulSoup(html, 'lxml')

video_url = soup.find(id='video').get('src')

return video_url

def download_file(url, file_path=None):

headers = {

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}

response = requests.get(url, headers=headers, stream=True)

if not file_path:

file_path = url.split('/')[-1]

with open(file_path, 'wb') as f:

for chunk in response.iter_content(1024):

f.write(chunk)

return file_path

```

The above code demonstrates how to get the video file from the video URL and how to download the file. This can be achieved by analyzing the raw HTML code.

2. Remove watermark

Once we have downloaded the corresponding image or video, we can use the OpenCV library to remove the watermark. Here is a simple example:

```python

import cv2

def remove_watermark(image_path, output_path):

img = cv2.imread(image_path)

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

blurred = cv2.GaussianBlur(gray, (11, 11), 0)

edged = cv2.Canny(blurred, 30, 150)

contours = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

contours = contours[0] if len(contours) == 2 else contours[1]

cv2.drawContours(img, contours, -1, (255, 255, 255), -1)

cv2.imwrite(output_path, img)

```

This code demonstrates how to use edge detection and contour extraction techniques to identify and remove watermarks from images. Note that this algorithm is not exhaustive and it may not be able to remove all types of watermarks.

For video, we need to do this by reading each frame of the video frame by frame. We can use the MoviePy library to achieve this functionality.

```python

import moviepy.editor as mp

def remove_watermark(video_path, output_path):

video = mp.VideoFileClip(video_path)

no_watermark = video.fl_image(remove_watermark_frame)

no_watermark.write_videofile(output_path)

def remove_watermark_frame(frame):

# Code to remove single frame watermark

return frame

```

The first piece of code defines a natural dewatermarking function, and the second piece of code uses the MoviePy library for frame-by-frame processing. Each frame is passed to the remove_watermark_frame function, which uses techniques similar to image watermarking to remove the watermark on each frame.

3. Generate shared pictures or videos

 

Guess you like

Origin blog.csdn.net/weixin_47245419/article/details/129917791