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

 更新时间:2021年01月22日 16:20:09   作者:遇见YY  
这篇文章主要介绍了python将YUV420P文件转PNG图片格式的两种方法,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下

方法一:

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中的round()方法

    简单介绍Python中的round()方法

    这篇文章主要介绍了简单介绍Python中的round()方法,是Python入门的基础知识,需要的朋友可以参考下
    2015-05-05
  • Python中的 ansible 动态Inventory 脚本

    Python中的 ansible 动态Inventory 脚本

    这篇文章主要介绍了Python中的 ansible 动态Inventory 脚本,本章节通过实例代码从mysql数据作为数据源生成动态ansible主机为入口介绍的非常详细,感兴趣的朋友跟随小编一起看看吧
    2020-01-01
  • python 提取文件指定列的方法示例

    python 提取文件指定列的方法示例

    这篇文章主要介绍了python 提取文件指定列的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-08-08
  • Matplotlib实战之面积图绘制详解

    Matplotlib实战之面积图绘制详解

    面积图,或称区域图,是一种随有序变量的变化,反映数值变化的统计图表,这篇文章主要介绍了如何利用Matplotlib实现面积图的绘制,需要的可以参考下
    2023-08-08
  • python 实现按对象传值

    python 实现按对象传值

    今天小编就为大家分享一篇python 实现按对象传值,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-12-12
  • M1芯片Mac上Anaconda的暂时替代(miniforge)

    M1芯片Mac上Anaconda的暂时替代(miniforge)

    这篇文章主要介绍了M1芯片Mac上Anaconda的暂时替代(miniforge),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • python 实现图像快速替换某种颜色

    python 实现图像快速替换某种颜色

    这篇文章主要介绍了python 实现图像快速替换某种颜色,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-06-06
  • CentOS中升级Python版本的方法详解

    CentOS中升级Python版本的方法详解

    本文给大家分享的是再centos系统中将Python版本从2.6升级到2.7的方法和升级过程中遇到的问题的处理,非常详细,有需要的小伙伴可以参考下
    2017-07-07
  • 浅谈python3中input输入的使用

    浅谈python3中input输入的使用

    这篇文章主要介绍了浅谈python3中input输入的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-08-08
  • Django结合使用Scrapy爬取数据入库的方法示例

    Django结合使用Scrapy爬取数据入库的方法示例

    这篇文章主要介绍了Django结合使用Scrapy爬取数据入库的方法示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03

最新评论