opencv常用操作
1、imdecode()与imencode() 图片的解码与编码
""" 图片解码 """ import cv2 import numpy as np import requests image_url = "https://bkimg.cdn.bcebos.com/pic/0823dd54564e92581460604f9082d158ccbf4e3f?x-bce-process=image/watermark,image_d2F0ZXIvYmFpa2U5Mg==,g_7,xp_5,yp_5" r = requests.get(image_url) img = np.asarray(bytearray(r.content), dtype='uint8') img = cv2.imdecode(img, cv2.IMREAD_COLOR) # 将数据解码为图片格式 # cv2.imshow('Crystal', img) # cv2.waitKey() cv2.imwrite("Crystal.jpg", img)
""" 图片编码 """ import numpy as np import urllib import cv2 img = cv2.imread("Crystal.jpg") img_bin_jpg = cv2.imencode('.jpg', img) # img_bin_png = cv2.imencode('.png', img) #不同格式图片 # print(img_bin_jpg) data_encode = np.array(img_bin_jpg) str_encode = data_encode.tostring() print(str_encode)