图像编码为bytes import cv2 # 指定编码为JPEG格式的,要和图片存储的格式一致 img_encode = cv2.imencode('.jpg', img)[1] data_encode = np.array(img_encode) str_encode = data_encode.tostring()