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编写一个在Linux下实现截图分享的脚本的教程
Apr 24 Python
Python加pyGame实现的简单拼图游戏实例
May 15 Python
Python初学时购物车程序练习实例(推荐)
Aug 08 Python
Python通过matplotlib绘制动画简单实例
Dec 13 Python
python使用pandas实现数据分割实例代码
Jan 25 Python
对python使用http、https代理的实例讲解
May 07 Python
在Python中将函数作为另一个函数的参数传入并调用的方法
Jan 22 Python
python接口自动化测试之接口数据依赖的实现方法
Apr 26 Python
使用Python-OpenCV消除图像中孤立的小区域操作
Jul 05 Python
如何使用 Flask 做一个评论系统
Nov 27 Python
Python matplotlib可视化之绘制韦恩图
Feb 24 Python
Python 第三方库 openpyxl 的安装过程
Dec 24 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
js和php邮箱地址验证的实现方法
2014/01/09 PHP
php动态生成函数示例
2014/03/21 PHP
php实现用于删除整个目录的递归函数
2015/03/16 PHP
thinkPHP5.0框架独立配置与动态配置方法
2017/03/17 PHP
详解PHP防止直接访问.php 文件的实现方法
2017/07/28 PHP
JavaScript的eval JSON object问题
2009/11/15 Javascript
基于jquery的一个浮动框(扩展性比较好 )
2010/08/27 Javascript
js下关于onmouseout、事件冒泡的问题经验小结
2010/12/09 Javascript
extjs表格文本启用选择复制功能具体实现
2013/10/11 Javascript
图片动画横条广告带上下滚动可自定义图片、链接等等
2013/10/20 Javascript
Jquery对数组的操作技巧整理
2014/03/25 Javascript
jquery使用hide方法隐藏指定id的元素
2015/03/30 Javascript
JS实现的论坛Ajax打分效果完整实例
2015/10/31 Javascript
javascript实现PC网页里的拖拽效果
2016/03/14 Javascript
深入理解逻辑表达式的用法 与或非的用法
2016/06/06 Javascript
javascript基础知识
2016/06/07 Javascript
JS针对Array的各种操作汇总
2016/11/29 Javascript
使用vue-router完成简单导航功能【推荐】
2018/06/28 Javascript
JavaScript引用类型Date常见用法实例分析
2018/08/08 Javascript
js prototype和__proto__的关系是什么
2019/08/23 Javascript
如何配置vue.config.js 处理static文件夹下的静态文件
2020/06/19 Javascript
python3音乐播放器简单实现代码
2020/04/20 Python
python中logging库的使用总结
2017/10/18 Python
python使用itchat库实现微信机器人(好友聊天、群聊天)
2018/01/04 Python
ZABBIX3.2使用python脚本实现监控报表的方法
2019/07/02 Python
Django 对IP访问频率进行限制的例子
2019/08/30 Python
Django在Model保存前记录日志实例
2020/05/14 Python
在django中查询获取数据,get, filter,all(),values()操作
2020/08/09 Python
python抢购软件/插件/脚本附完整源码
2021/03/04 Python
中国宠物用品商城:E宠商城
2016/08/27 全球购物
AJAX的全称是什么
2012/11/06 面试题
董事长岗位职责
2013/11/30 职场文书
中层干部培训方案
2014/06/16 职场文书
小学生清明节演讲稿
2014/09/05 职场文书
《揠苗助长》教学反思
2016/02/20 职场文书
python spilt()分隔字符串的实现示例
2021/05/21 Python