
去水印python

在 Python 中去除水印可以根據水印類型(圖片、文字、視頻)和復雜度選擇不同的工具和算法。以下是常見場景的解決方案:一、靜態圖片去水印1. 基礎圖像處理(簡單水印)...
在 Python 中去除水印可以根據水印類型(圖片、文字、視頻)和復雜度選擇不同的工具和算法。以下是常見場景的解決方案:
一、靜態圖片去水印
1. 基礎圖像處理(簡單水印)
```python
from PIL import Image, ImageEnhance
def remove_watermark(input_path, output_path):
img = Image.open(input_path)
檢測水印區域(需預先知道位置)
watermark = img.crop((left, top, right, bottom))
img.paste(watermark, (0,0), watermark)
img.save(output_path)
```
2. 智能去水印(深度學習)
使用預訓練模型(需安裝 pytorch
):
```python
import torch
from remove.bg import Remove
def remove_bg(input_path):
remove = Remove()
with open(input_path, "rb") as f:
img = remove.remove(f)
img.save("output.png")
```
3. OCR + 圖像修復(文字水印)
```python
import easyocr
from PIL import ImageEnhance
def remove_text_watermark(input_path):
OCR 識別文字區域
reader = easyocr.Reader(['ch_sim', 'en'])
result = reader.readtext(input_path)
獲取水印坐標并修復img = Image.open(input_path)
for det in result:
if det[4] > 0.7: 置信度判斷
left, top, right, bottom, conf = det[:5]
img = img.crop((left, top, right, bottom))
img.save("output.png")
```
二、視頻去水印
1. FFmpeg + Python
```python
import subprocess
def remove_video_watermark(input_path, output_path):
cmd = [
'ffmpeg', '-i', input_path,
'-vf', 'movie=watermark.png@0.5', 添加半透明水印(示例)
output_path
]
subprocess.run(cmd)
```
2. 實時去水印(需GPU)
```python
import torch
from videoai import VideoAI
def real_time_remove(input_path):
videoai = VideoAI('watermark_remove_model')
videoai.remove_watermark(input_path, output_path)
```
三、API 服務集成
- Remove.bg API
```python
import requests
def remove_bg_api(input_path):
with open(input_path, "rb") as f:
response = requests.post(
"https://api.remove.bg/v1.0/removebg",
files={"image": f
本文鏈接:http://www.wzyaohuidianqi.cn/gong/446283.html
