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入门教程之识别验证码
Mar 04 Python
Python使用filetype精确判断文件类型
Jul 02 Python
python使用socket创建tcp服务器和客户端
Apr 12 Python
对python3标准库httpclient的使用详解
Dec 18 Python
Python简单获取二维数组行列数的方法示例
Dec 21 Python
python 搭建简单的http server,可直接post文件的实例
Jan 03 Python
Python常见数据结构之栈与队列用法示例
Jan 14 Python
python调用动态链接库的基本过程详解
Jun 19 Python
Python3 使用pillow库生成随机验证码
Aug 26 Python
python网络爬虫 CrawlSpider使用详解
Sep 27 Python
python输出数学符号实例
May 11 Python
Anaconda使用IDLE的实现示例
Sep 23 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
消息持续发送的完整例子
2006/10/09 PHP
PHP 中的面向对象编程:通向大型 PHP 工程的办法
2006/12/03 PHP
PHP程序员必须清楚的问题汇总
2014/12/18 PHP
php有道翻译api调用方法实例
2014/12/22 PHP
php中mail函数发送邮件失败的解决方法
2014/12/24 PHP
深入浅出讲解:php的socket通信原理
2016/12/03 PHP
ThinkPHP5.0框架控制器继承基类和自定义类示例
2018/05/25 PHP
php + ajax 实现的写入数据库操作简单示例
2020/05/16 PHP
javascript-TreeView父子联动效果保持节点状态一致
2007/08/12 Javascript
JavaScript中的字符串操作详解
2013/11/12 Javascript
javascript中setTimeout的问题解决方法
2014/05/08 Javascript
jQuery使用元素属性attr赋值详解
2015/02/27 Javascript
layui文件上传实现代码
2017/05/20 Javascript
js实现rem自动匹配计算font-size的示例
2017/11/18 Javascript
vue filters的使用详解
2018/06/11 Javascript
微信小程序实现收藏与取消收藏切换图片功能
2018/08/03 Javascript
详解Node.js异步处理的各种写法
2019/06/09 Javascript
Vue——解决报错 Computed property &quot;****&quot; was assigned to but it has no setter.
2020/12/19 Vue.js
wxpython 最小化到托盘与欢迎图片的实现方法
2014/06/09 Python
python3实现TCP协议的简单服务器和客户端案例(分享)
2017/06/14 Python
Python实现的文轩网爬虫完整示例
2019/05/16 Python
python实现感知机线性分类模型示例代码
2019/06/02 Python
Python 实现数据结构-循环队列的操作方法
2019/07/17 Python
opencv调整图像亮度对比度的示例代码
2019/09/27 Python
python之array赋值技巧分享
2019/11/28 Python
python自动识别文本编码格式代码
2019/12/26 Python
使用python处理题库表格并转化为word形式的实现
2020/04/14 Python
pygame用blit()实现动画效果的示例代码
2020/05/28 Python
英国骑行、跑步、游泳、铁人三项运动装备专卖店:Wiggle
2016/08/23 全球购物
巴西最大的玩具连锁店:Ri Happy
2020/06/17 全球购物
上海奥佳笔试题面试题
2016/11/16 面试题
温馨提示标语
2014/06/26 职场文书
医生学习党的群众路线教育实践活动心得体会
2014/11/03 职场文书
PyTorch 如何自动计算梯度
2021/05/23 Python
php修改word的实例方法
2021/11/17 PHP
HTML静态页面获取url参数和UserAgent的实现
2022/08/05 HTML / CSS