import ssl
import urllib.request
import cv2
import numpy as np
#获得图片
def getPic(name,url):
# name ---图片地址
# url ---图片名字(自定义)
# 解决SSL验证
ssl._create_default_https_context = ssl._create_unverified_context
# 请求图片
resp = urllib.request.urlopen(url)
image = np.asarray(bytearray(resp.read()), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
cv2.imwrite(name, image)
# cv2.imshow(name, image)
# cv2.waitKey()
return image
#装饰图转化为二值图
def getBinaryImage(image):
#如果 src(x,y)>threshold,dst(x,y) = 0; 否则,dst(x,y) = max_value
#像素>0 --> = 0
#像素=0 --> = 255
return cv2.threshold(image,0,255,cv2.THRESH_BINARY_INV)
#原始图像地址
img_src_test='https://192.168.2.31:6443/arcgis/rest/services/XZ_Clip/MapServer/tile/0/11957/16689'
#装饰图地址
img_src_mask = 'https://192.168.2.31:6443/arcgis/rest/services/XZ_Ct/MapServer/tile/0/11957/16689'
#得到2张图片
image_test=getPic('img_src_test.jpg',img_src_test)
image_mask=getPic('img_src_mask.jpg',img_src_mask)
#将装饰图转为二值图
ret,image_mask = getBinaryImage(image_mask)
#展示转化后的二值图
# cv2.imshow("Image", image_mask)
# cv2.waitKey(500000)
#相加操作
image=cv2.add(image_mask,image_test)
cv2.imshow("Image", image)
cv2.waitKey(0)
结果图: