Python学习去水印遇到的问题

96 阅读1分钟
import cv2
import numpy as np
from PIL import Image
import os
 
dir = os.getcwd()
path = "E:\\migu\\test_project\\test-clipcloud-tool\\src\\xiaohongshu\\01.png"
newPath = "new.jpg"
img=cv2.imread(path,1)
hight,width,depth=img.shape[0:3]
 
#截取
cropped = img[int(hight*0.8):hight, int(width*0.7):width]  # 裁剪坐标为[y0:y1, x0:x1]
cv2.imwrite(newPath, cropped)
imgSY = cv2.imread(newPath,1)
 
#图片二值化处理,把[200,200,200]-[250,250,250]以外的颜色变成0
thresh = cv2.inRange(imgSY,np.array([200,200,200]),np.array([250,250,250]))
#创建形状和尺寸的结构元素
kernel = np.ones((3,3),np.uint8)
#扩展待修复区域
hi_mask = cv2.dilate(thresh,kernel,iterations=10)
specular = cv2.inpaint(imgSY,hi_mask,5,flags=cv2.INPAINT_TELEA)
cv2.imwrite(newPath, specular)
 
#覆盖图片
imgSY = Image.open(newPath)
img = Image.open(path)
img.paste(imgSY, (int(width*0.7),int(hight*0.8),width,hight))
img.save(newPath)

image.png

第一次运行报此错误ModuleNotFoundError: No module named ‘cv2’

是因为没有下载此模块

导入包虽然是cv2但是下载要下载**pip install opencv_python**

直接下载可能不行,可能会下载失败

Traceback (most recent call last):
File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pip/_vendor/urllib3/response.py”, line 425, in _error_catcher
yield
File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pip/_vendor/urllib3/response.py”, line 507, in read
data = self._fp.read(amt) if not fp_closed else b""
File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py”, line 457, in read
n = self.readinto(b)
File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/client.py”, line 501, in readinto
n = self.fp.readinto(b)
File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socket.py”, line 589, in readinto
return self._sock.recv_into(b)
File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py”, line 1071, in recv_into
return self.read(nbytes, buffer)
File “/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/ssl.py”, line 929, in read
return self._sslobj.read(len, buffer)
socket.timeout: The read operation timed out
————————————————
版权声明:本文为CSDN博主「赐福包」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Zcincn/article/details/106363858

可以使用如下方法,加上镜像地址【这里使用的清华镜像】

pip install opencv_python -i pypi.tuna.tsinghua.edu.cn/simple