方法一:
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
· 零经验选手,Compose 一天开发一款小游戏!