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中3种内建数据结构:列表、元组和字典
Nov 30 Python
分享Python文本生成二维码实例
Jan 06 Python
opencv python 基于KNN的手写体识别的实例
Aug 03 Python
Python装饰器模式定义与用法分析
Aug 06 Python
python 切换root 执行命令的方法
Jan 19 Python
python3利用ctypes传入一个字符串类型的列表方法
Feb 12 Python
TensorFlow实现简单的CNN的方法
Jul 18 Python
python实现对图片进行旋转,放缩,裁剪的功能
Aug 07 Python
python双端队列原理、实现与使用方法分析
Nov 27 Python
python-视频分帧&amp;多帧合成视频实例
Dec 10 Python
运行Python编写的程序方法实例
Oct 21 Python
python3实现飞机大战
Nov 29 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
Windows下PHP的任意文件执行漏洞
2006/10/09 PHP
php中一个有意思的日期逻辑处理
2012/03/25 PHP
PhpStorm本地断点调试的方法步骤
2018/05/21 PHP
微信公众平台开发教程②微信端分享功能图文详解
2019/04/10 PHP
JS脚本defer的作用示例介绍
2014/01/02 Javascript
JQuery实现防止退格键返回的方法
2015/02/12 Javascript
JavaScript实现的经典文件树菜单效果
2015/09/08 Javascript
jQuery实现调整表格单列顺序完整实例
2016/06/20 Javascript
node.js中module.exports与exports用法上的区别
2016/09/02 Javascript
浅谈angularjs module返回对象的坑(推荐)
2016/10/21 Javascript
jQuery使用正则验证15/18身份证的方法示例
2017/04/27 jQuery
JavaScript中undefined和null的区别
2017/05/03 Javascript
JavaScript与Java正则表达式写法的区别介绍
2017/08/15 Javascript
微信小程序tabBar模板用法实例分析【附demo源码下载】
2017/11/28 Javascript
浅析Node.js非对称加密方法
2018/01/29 Javascript
快速解决bootstrap下拉菜单无法隐藏的问题
2018/08/10 Javascript
jQuery/JS监听input输入框值变化实例
2019/10/17 jQuery
vue实现图片上传功能
2020/05/28 Javascript
TypeScript 运行时类型检查补充工具
2020/09/28 Javascript
Python使用scrapy采集时伪装成HTTP/1.1的方法
2015/04/08 Python
深入解析Python中的lambda表达式的用法
2015/08/28 Python
Python中matplotlib中文乱码解决办法
2017/05/12 Python
python下setuptools的安装详解及No module named setuptools的解决方法
2017/07/06 Python
利用Python2下载单张图片与爬取网页图片实例代码
2017/12/25 Python
pyqt5 QScrollArea设置在自定义侧(任何位置)
2019/09/25 Python
PyQt5+python3+pycharm开发环境配置教程
2020/03/24 Python
Python验证码截取识别代码实例
2020/05/16 Python
python为什么要安装到c盘
2020/07/20 Python
纯css3实现的竖形无限级导航
2014/12/10 HTML / CSS
基于zepto的插件之移动端无缝向上滚动并上下触摸滑动实例代码
2016/12/20 HTML / CSS
Original Penguin美国官网:布拉德皮特、强尼德普喜爱的服装品牌
2016/10/25 全球购物
惠普墨西哥官方商店:HP墨西哥
2016/12/01 全球购物
酒店服务实习自我鉴定
2013/09/22 职场文书
后勤人员岗位职责
2013/12/17 职场文书
2016年高校自主招生自荐信范文
2015/03/24 职场文书
Python中使用ipython的详细教程
2021/06/22 Python