DDR爱好者之家 Design By 杰米
方法一:
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的资料请关注其它相关文章!
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2025年10月25日
2025年10月25日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]