opencv读取和存取图片字节数据
图片转字节
img = cv2.imread(img_path) imgbytes = cv2.imencode(".jpg", img)[1].tobytes()
字节转图片
#先读取图片为字节,再转图片array
with open(img_file, "rb") as f: img_data = f.read() img = cv2.imdecode(np.fromstring(img_data, dtype=np.uint8), 1)
#采用numpy的fromfile
img_path = r"test.jpg"
img = cv2.imdecode(np.fromfile(img_path, dtype=np.uint8), 1)
读取中文路径
img = cv2.imdecode(np.fromfile(img_file, dtype=np.uint8), 1)
写入中文路径
cv2.imencode('.jpg', new_img)[1].tofile(img_path)

浙公网安备 33010602011771号