python将YUV420P文件转PNG图片格式
2021-01-08 04:29
标签:memset class only htm main col sprintf bsp void 方法一: 方法二: highway视频网址:http://trace.eas.asu.edu/yuv/index.html 附录:将yuv文件转化为一帧帧yuv文件 python将YUV420P文件转PNG图片格式 标签:memset class only htm main col sprintf bsp void 原文地址:https://www.cnblogs.com/iuyy/p/14238301.htmlimport os
import cv2 as cv
import numpy as np
# 读取yuv420p的一帧文件,并转化为png图片
if __name__ == ‘__main__‘:
filepath = ‘one_frame_of_highway.yuv‘
binfile = open(filepath, ‘rb‘)
size = os.path.getsize(filepath)
image_width = 352
image_hight = 288
image_y = [[0] * image_width for i in range(image_hight)]
image_u = [[0] * image_width for i in range(image_hight)]
image_v = [[0] * image_width for i in range(image_hight)]
for r in range(image_hight):
for c in range(image_width):
image_y[r][c] = binfile.read(1)[0]
Image_Y = np.array(image_y)
for r in range(int(image_hight / 2)):
for c in range(int(image_width / 2)):
pixel = binfile.read(1)[0]
image_u[2 * r + 0][2 * c + 0] = pixel
image_u[2 * r + 1][2 * c + 0] = pixel
image_u[2 * r + 0][2 * c + 1] = pixel
image_u[2 * r + 1][2 * c + 1] = pixel
Image_U = np.array(image_u)
for r in range(int(image_hight / 2)):
for c in range(int(image_width / 2)):
pixel = binfile.read(1)[0]
image_v[2 * r + 0][2 * c + 0] = pixel
image_v[2 * r + 0][2 * c + 1] = pixel
image_v[2 * r + 1][2 * c + 0] = pixel
image_v[2 * r + 1][2 * c + 1] = pixel
Image_V = np.array(image_v)
binfile.close()
compose = np.array([Image_Y, Image_V, Image_U]).transpose([1, 2, 0]).astype(np.uint8)
Image = cv.cvtColor(compose, cv.COLOR_YUV2RGB)
cv.imwrite("one_frame_of_highway.yuv.png", Image)
ffmpeg -s 352x288 -i one_frame_of_highway.yuv one_frame_of_highway.png
#include
下一篇:Java基础__03.异常
文章标题:python将YUV420P文件转PNG图片格式
文章链接:http://soscw.com/index.php/essay/40854.html