最近项目需要,缩小图像后同时需要矩阵和byte图像,这里记录一下转换代码
def resize_img_1280p(file_path):
img= cv2.imread(file_path)
h,w = img.shape[:2]
new_h = max(h,w)
fx = 1280/new_h
#按比例缩小图像
resize_img = cv2.resize(img,(0, 0), fx = fx, fy = fx, interpolation = cv2.INTER_LINEAR)
#转换为byte
img_encode = cv2.imencode('.jpg', resize_img)
str_encode = img_encode[1].tostring()
return resize_img, str_encode
这样就可以同时返回了。