欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-01-10 19:54:16
方法一:import osimport cv2 as cvimport numpy as np# 读取yuv420p的一帧文件,并转化为png图片if __name__ == '__main__':...

方法一:

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视频网址:

附录:

将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的资料请关注其它相关文章!