python将YUV420P文件转PNG图片格式的两种方法


Posted in Python onJanuary 22, 2021

方法一:

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;
}

以上就是python将YUV420P文件转PNG图片格式的两种方法的详细内容,更多关于python将YUV420P文件转PNG的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
python求素数示例分享
Feb 16 Python
Python中的map、reduce和filter浅析
Apr 26 Python
Python的Urllib库的基本使用教程
Apr 30 Python
Python实现telnet服务器的方法
Jul 10 Python
python tkinter实现界面切换的示例代码
Jun 14 Python
numpy和pandas中数组的合并、拉直和重塑实例
Jun 28 Python
python 提取文件指定列的方法示例
Aug 07 Python
简单了解python filter、map、reduce的区别
Jan 14 Python
Python PyPDF2模块安装使用解析
Jan 19 Python
解决pycharm debug时界面下方不出现step等按钮及变量值的问题
Jun 09 Python
Python编写nmap扫描工具
Jul 21 Python
Python制作春联的示例代码
Jan 22 Python
如何使用Python进行PDF图片识别OCR
Jan 22 #Python
详解pandas映射与数据转换
Jan 22 #Python
python实现简单的井字棋游戏(gui界面)
Jan 22 #Python
Django url 路由匹配过程详解
Jan 22 #Python
浅析pandas随机排列与随机抽样
Jan 22 #Python
python 合并多个excel中同名的sheet
Jan 22 #Python
Python读取pdf表格写入excel的方法
Jan 22 #Python
You might like
php+mysql大量用户登录解决方案分析
2014/12/29 PHP
php 升级到 5.3+ 后出现的一些错误,如 ereg(); ereg_replace(); 函数报错
2015/12/07 PHP
CI框架中redis缓存相关操作文件示例代码
2016/05/17 PHP
js中top/parent/frame概述及案例应用
2013/02/06 Javascript
javascript相等运算符与等同运算符详细介绍
2013/11/09 Javascript
Javascript中的String对象详谈
2014/03/03 Javascript
jQuery 鼠标经过(hover)事件的延时处理示例
2014/04/14 Javascript
JS中产生20位随机数以0-9为例也可以是a-z A-Z
2014/08/01 Javascript
理解javascript回调函数
2014/12/28 Javascript
jQuery实现输入框下拉列表树插件特效代码分享
2015/08/27 Javascript
详解JavaScript表单验证(E-mail 验证)
2016/03/31 Javascript
前端性能优化及技巧
2016/05/06 Javascript
js和jQuery设置Opacity半透明 兼容IE6
2016/05/24 Javascript
js简易版购物车功能
2017/06/17 Javascript
详解a++和++a的区别
2017/08/30 Javascript
vue2.0 和 animate.css的结合使用
2017/12/12 Javascript
Angular实现的日程表功能【可添加及隐藏显示内容】
2017/12/27 Javascript
vue中利用simplemde实现markdown编辑器(增加图片上传功能)
2019/04/29 Javascript
js实现随机点名器精简版
2020/06/29 Javascript
[44:41]Fnatic vs Liquid 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
总结Python编程中三条常用的技巧
2015/05/11 Python
开始着手第一个Django项目
2015/07/15 Python
Python操作SQLite数据库的方法详解
2017/06/16 Python
python找出完数的方法
2018/11/12 Python
python全栈要学什么 python全栈学习路线
2019/06/28 Python
Python + Requests + Unittest接口自动化测试实例分析
2019/12/12 Python
Python定义函数时参数有默认值问题解决
2019/12/19 Python
Pyinstaller打包Scrapy项目的实现步骤
2020/09/22 Python
Python学习之time模块的基本使用
2021/01/17 Python
HTML5 video循环播放多个视频的方法步骤
2020/08/06 HTML / CSS
大学生大二自我鉴定
2013/10/28 职场文书
银行实习推荐信
2015/03/27 职场文书
2015年银行客户经理工作总结
2015/04/01 职场文书
实施意见格式范本
2015/06/05 职场文书
我的暑假生活作文(五年级)范文
2019/08/07 职场文书
JavaScript实现一键复制内容剪贴板
2022/07/23 Javascript