【Python脚本】批量将二进制格式图片转换成png或mp4格式

# coding=utf-8
import cv2
import numpy as np
import os


def convert_bin2png():
    """把单通道的bin文件转换为png"""
    for i in range(pic_num):
        bin_name = "{}.bin".format(i)
        input_name = path + bin_name
        image = np.fromfile(input_name, dtype=np.uint8)
        image_gray = np.reshape(image, (height, width, channel))
        output_name = "png/{}.png".format(i)  # 需在输入文件夹中新建子文件夹png,作为输出图片路径
        out_path_name = path + output_name  # 输出图片路径
        cv2.imwrite(out_path_name, image_gray)
        print("already wrote", i + 1, "/", pic_num, "pictures")  # 浏览进度


def convert_bin2video():
    """"""
    size = (width, height)  # 保存图片的尺寸
    fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
    video_writer = cv2.VideoWriter(path + '_out_video.avi', fourcc, fps, size)
    for i in range(0, pic_num):
        bin_name = "{}.bin".format(i)
        input_name = path + bin_name
        image = np.fromfile(input_name, dtype=np.uint8)
        frame = np.reshape(image, (height, width, channel))
        video_writer.write(frame)
        print("converting", i, "th picture to video")
    video_writer.release()


if __name__ == '__main__':
    path = "E:/pan_backup/2021-11-01/vpu_mask_1/image/"  # 输入bin文件的路径
    pic_num = len(os.listdir(path))  # 自动获取图片数量
    width = 720  # 图片宽度
    height = 720  # 图片高度
    channel = 3  # 通道数
    fps = 30  # 保存视频的FPS,30:正常速度, 60:2倍速
    # 转换成图片帧
    # convert_bin2png()
    # 转换成视频
    convert_bin2video()

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值