# 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()
【Python脚本】批量将二进制格式图片转换成png或mp4格式
最新推荐文章于 2024-04-10 11:25:41 发布