python将YUV420P文件转PNG图片格式的两种方法
作者:遇见YY 时间:2021-09-04 10:06:10
方法一:
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 <stdio.h>
#include <fcntl.h>
#include <zconf.h>
#include <stdint.h>
#include <strings.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
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;
}
来源:https://www.cnblogs.com/iuyy/p/14238301.html
标签:python,YUV420P,PNG
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python数据类型之Tuple元组实例详解
2023-02-17 05:24:01
Python排序算法之堆排序算法
2023-01-17 02:00:55
![](https://img.aspxhome.com/file/2023/3/63933_0s.png)
PHP访问MySQL查询超时处理的方法
2023-11-23 03:05:48
![](https://img.aspxhome.com/file/2023/2/85812_0s.jpg)
在django中使用post方法时,需要增加csrftoken的例子
2023-08-12 06:44:34
python for循环赋值问题
2023-01-26 05:56:32
Python中Tkinter Scrollbar滚动条(窗口滑动条)
2021-11-12 00:22:59
![](https://img.aspxhome.com/file/2023/1/103491_0s.jpg)
python利用标准库如何获取本地IP示例详解
2021-10-17 07:46:07
使用python实现定时报天气的示例代码
2021-12-27 14:56:10
![](https://img.aspxhome.com/file/2023/9/105199_0s.png)
利用Python抢回在蚂蚁森林逝去的能量(实现代码)
2022-07-01 15:15:39
![](https://img.aspxhome.com/file/2023/6/90646_0s.jpg)
js实时获得服务器上时间
2008-11-25 13:55:00
python selenium参数详解和实现案例
2023-09-25 01:20:29
![](https://img.aspxhome.com/file/2023/1/62881_0s.png)
Django模板过滤器和继承示例详解
2023-10-25 16:52:56
![](https://img.aspxhome.com/file/2023/3/75913_0s.png)
如何显示数据库里的图片?
2010-06-08 09:36:00
那些看一眼就让你难忘的广告
2007-09-21 19:46:00
![](https://img.aspxhome.com/file/UploadPic/20079/21/2007921195335406s.jpg)
PLSQL导入dmp文件的详细完整步骤
2023-06-26 11:45:32
![](https://img.aspxhome.com/file/2023/6/63716_0s.png)
从IIS到SQL Server数据库安全
2008-12-24 15:58:00
ASP控制每页打印行数实例
2008-04-13 06:48:00
谈谈Javascript中的++和–操作符
2009-05-08 11:43:00
Python如何获取pid和进程名字
2023-11-11 11:44:11
![](https://img.aspxhome.com/file/2023/8/78458_0s.jpg)
python 实现list或string按指定分段
2023-10-30 02:04:20