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使用MD5加密字符串示例
Aug 22 Python
Python实现比较两个列表(list)范围
Jun 12 Python
在Python中移动目录结构的方法
Jan 31 Python
Python开发微信公众平台的方法详解【基于weixin-knife】
Jul 08 Python
python pandas消除空值和空格以及 Nan数据替换方法
Oct 30 Python
Django之使用celery和NGINX生成静态页面实现性能优化
Oct 08 Python
Python使用pymysql模块操作mysql增删改查实例分析
Dec 19 Python
在PyCharm中遇到pip安装 失败问题及解决方案(pip失效时的解决方案)
Mar 10 Python
Tensorflow中的dropout的使用方法
Mar 13 Python
使用Numpy对特征中的异常值进行替换及条件替换方式
Jun 08 Python
pycharm导入源码的具体步骤
Aug 04 Python
python如何在word中存储本地图片
Apr 07 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 移除数组重复元素的一点说明
2008/11/27 PHP
PHP实现单文件、多个单文件、多文件上传函数的封装示例
2019/09/02 PHP
为Yahoo! UI Extensions Grid增加内置的可编辑器
2007/03/10 Javascript
JSON扫盲帖 JSON.as类教程
2009/02/16 Javascript
js 表单验证方法(实用)
2009/04/28 Javascript
nodejs 整合kindEditor实现图片上传
2015/02/03 NodeJs
使用jquery插件qrcode生成二维码
2015/10/22 Javascript
浅谈angularjs $http提交数据探索
2017/01/20 Javascript
详解Angularjs在控制器(controller.js)中使用过滤器($filter)格式化日期/时间实例
2017/02/17 Javascript
vue-router路由简单案例介绍
2017/02/21 Javascript
详解如何提高 webpack 构建 Vue 项目的速度
2017/07/03 Javascript
Python多线程编程(六):可重入锁RLock
2015/04/05 Python
Python中pygame的mouse鼠标事件用法实例
2015/11/11 Python
Python实现的文本编辑器功能示例
2017/06/30 Python
[原创]使用豆瓣提供的国内pypi源
2017/07/02 Python
shell命令行,一键创建 python 模板文件脚本方法
2018/03/20 Python
Python中py文件引用另一个py文件变量的方法
2018/04/29 Python
python简单操作excle的方法
2018/09/12 Python
pycharm修改界面主题颜色的方法
2019/01/17 Python
python3.7简单的爬虫实例详解
2019/07/08 Python
python 数据库查询返回list或tuple实例
2020/05/15 Python
解决Python中导入自己写的类,被划红线,但不影响执行的问题
2020/07/13 Python
python 19个值得学习的编程技巧
2020/08/15 Python
英国网上花店:Bunches
2016/11/29 全球购物
求职信模板标准格式范文
2014/02/23 职场文书
和解协议书
2014/04/16 职场文书
会计系毕业求职信
2014/08/07 职场文书
民事和解协议书格式
2014/11/29 职场文书
2014年小学数学教师工作总结
2014/12/03 职场文书
感谢信怎么写
2015/01/21 职场文书
检讨书范文300字
2015/01/28 职场文书
2015年教师节演讲稿范文
2015/03/19 职场文书
中考百日冲刺决心书
2015/09/22 职场文书
详解Python中下划线的5种含义
2021/07/15 Python
OpenCV 图像梯度的实现方法
2021/07/25 Python
一文搞懂Java中的注解和反射
2022/06/21 Java/Android