*.rgb转换为*.jpg的操作

记录一下图像转格式的代码,方便下次查阅:

同事转换图像和保存图像的代码:

void Rgb2Gray(unsigned char* rgb_data, unsigned char* gray_data, int width, int height)
{
	unsigned int gray_sum = 0;

	int i = 0;

	int j = 0;

	for (i = 0; i < height; i++)
	{
		for (j = 0; j < width; j++)
		{
			*gray_data++ = (28 * (*(rgb_data + 2)) + 151 * (*(rgb_data + 1)) + 77 * (*rgb_data)) >> 8;

			*gray_data++ = (28 * (*(rgb_data + 2)) + 151 * (*(rgb_data + 1)) + 77 * (*rgb_data)) >> 8;

			*gray_data++ = (28 * (*(rgb_data + 2)) + 151 * (*(rgb_data + 1)) + 77 * (*rgb_data)) >> 8;

			rgb_data += 3;
		}
	}
}

 

void Save2File(unsigned char* file_buffer, unsigned long buffer_size, int width, int height, unsigned int frame_index, std::string imgformat)
{
	if (!file_buffer)
	{
		TRACK_LOG(LOG_ERROR, "param error, pBuffer:%p, size:%d\n", file_buffer, buffer_size);

		return;
	}

	char fileName[250] = { 0 };

	time_t tt = time(0);

	struct tm* tmv = localtime(&tt);

	snprintf(fileName, sizeof(fileName), "C:\\temp\\%04d-%02d-%02d_%02d-%02d-%02d-%dx%d_%03d.%s",tmv->tm_year + 1900, tmv->tm_mon + 1, tmv->tm_mday, tmv->tm_hour, tmv->tm_min, tmv->tm_sec, width, height, frame_index, imgformat.c_str());

	TRACK_LOG(LOG_INFO, "filename: %s, buffer:%p, dataLen:%lu\n", fileName, file_buffer, buffer_size);

	FILE* fp = fopen(fileName, "wb+");

	if (fp)
	{
		fwrite(file_buffer, 1, buffer_size, fp);

		fflush(fp);

		fclose(fp);

		fp = NULL;
	}

}

同事使用的C++,我这边无法直接查看,所以更换图像的格式:

代码:

#filename = os.path.join(img_dir, file)
binFile = open('2022-12-27_14-41-23-1920x1080_037.rgb', "rb")
data = binFile.read()
data = [int(x) for x in data]
# 图像尺寸是需要预先知道的
empty_img = np.array(data).reshape((1080, 1920, 3)).astype(np.uint8)
save_path = os.path.join(save_dir, file[:-4]+'.jpg')
cv2.imwrite(save_path, empty_img)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猫猫与橙子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值