读取图片:

 img_gt = cv2.imdecode(np.fromfile(path, dtype=np.uint8), -1)
 img_gt = cv2.cvtColor(img_gt, cv2.IMREAD_COLOR)

写入图片:

write_path=f'{save_dir}/{imgname}.jpg'
cv2.imencode('.jpg', output)[1].tofile(write_path)  # 保存图片

C++版本

#include<iostream> 
#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include <opencv2/highgui/highgui_c.h>
using namespace cv;
int main()
{
	std::string image_path = samples::findFile("D:\\素材\\1.png");
	Mat img = imread(image_path, IMREAD_COLOR);
	imshow("src", img);//原图像
	waitKey(0);
}

在这里插入图片描述

更多推荐