利用python怎么将YUV420P文件转换成PNG图片格式-创新互联
这篇文章给大家介绍利用python怎么将YUV420P文件转换成PNG图片格式,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
高坪ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18982081108(备注:SSL证书合作)期待与您的合作!方法一:
import 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
highway视频网址:http://trace.eas.asu.edu/yuv/index.html
附录:
将yuv文件转化为一帧帧yuv文件
#include#include #include #include #include #include #include #include #include #include #include int File_Size(int fd) { struct stat st; fstat(fd, &st); return st.st_size; } int Frame_Size_Of_Cif() { int width = 352; int heigh = 288; int Y_SIZE = width * heigh; int U_SIZE = Y_SIZE / 4; int V_SIZE = Y_SIZE / 4; int Frame_SIZE = Y_SIZE + U_SIZE + V_SIZE; return Frame_SIZE; } int Frames_Of_Cif_File(int fd) { if (fd < 0) { printf("Invalid FD!"); return -1; } int Frame_SIZE = Frame_Size_Of_Cif(); int fd_size = File_Size(fd); return fd_size / Frame_SIZE; } void Abstract_Frame_From_CIF_File(int fd,char *Path_And_Prefix_Img,int Len) { int Frame_SIZE = Frame_Size_Of_Cif(); char file[128]; memset(file,0,128); memcpy(file,Path_And_Prefix_Img,Len); uint8_t buf[Frame_SIZE]; int ret = -1; int frames = 0; while ((ret = read(fd, buf, Frame_SIZE))) { frames += 1; uint64_t len = strlen(file); sprintf(file + len, "%d", frames); len = strlen(file); sprintf(file + len, "%s", ".yuv"); int fdw = open(file, O_RDWR | O_CREAT, 0777); write(fdw, buf, ret); memset(file,0,128); memcpy(file,Path_And_Prefix_Img,Len); close(fdw); } printf("Abstract %d frames!\n", frames); } int main() { int fd = open("./yuv420p_352x288.yuv", O_RDONLY); Abstract_Frame_From_CIF_File(fd,"/home/liu/Frames/Frames_",strlen("/home/liu/Frames/Frames_")); close(fd); return 0; }
关于利用python怎么将YUV420P文件转换成PNG图片格式就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
标题名称:利用python怎么将YUV420P文件转换成PNG图片格式-创新互联
转载来于:http://ybzwz.com/article/dopoei.html